EGGs Are Adhesive!
Abstract
The use of rewriting-based visual formalisms is on the rise. In the formal methods community, this is due also to the introduction of adhesive categories, where most properties of classical approaches to graph transformation, such as those on parallelism and confluence, can be rephrased and proved in a general and uniform way. E-graphs (EGGs) are a formalism for program optimisation via an efficient implementation of equality saturation. In short, EGGs can be defined as (acyclic) term graphs with an additional notion of equivalence on nodes that is closed under the operators of the signature. Instead of replacing the components of a program, the optimisation step is performed by adding new components and linking them to the existing ones via an equivalence relation, until an optimal program is reached. This work describes EGGs via adhesive categories. Besides the benefits in itself of a formal presentation, which renders precise the properties of the data structure, the description of the addition of equivalent program components using standard graph transformation tools offers the advantages of the adhesive framework in modelling, for example, concurrent updates.
Keywords and phrases:
Hypergraphs, terms graphs, e-graphs, adhesive categoriesCopyright and License:
![[Uncaptioned image]](x1.png) © Roberto Biondo, Davide Castelnovo, and Fabio Gadducci; licensed under Creative Commons License CC-BY 4.0
 © Roberto Biondo, Davide Castelnovo, and Fabio Gadducci; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Models of computation ; Theory of computation Semantics and reasoningFunding:
The research has been partially supported by the Italian MUR - Call PRIN 2022 - Project 20228KXFN2 “Spatio-Temporal Enhancement of Neural nets for Deeply Hierarchical Automatised Logic” (STENDHAL) and by the University of Pisa - Call PRA 2022 - Project 2022_99 “Formal Methods for the Healthcare Domain based on Spatial Information” (FM4HD).Editors:
Corina Cîrstea and Alexander KnappSeries and Publisher:
 Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
 Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
The introduction of adhesive categories marked a watershed moment for the algebraic approaches to the rewriting of graph-like structures [25, 17]. Until then, key results of the approaches on e.g. parallelism and confluence had to be proven over and over again for each different formalism at hand, despite the obvious similarity of the procedure. Adhesive categories provides such a disparate set of formalisms with a common abstract framework where many of these general results can be recast and uniformly proved once and for all. In short, following the double-pushout (DPO) approach to graph transformation [13, 17], a rule is given by two arrows and and its application requires a match : the rewriting step from to is then given by the diagram below, whose halves are pushouts.
Thus, and are the left- and right-hand side of the rule, respectively, while witnesses those parts that must be present for the rule to be executed, yet that are not affected by the rule itself. Should a category be adhesive, and the arrows of the rules monomorphisms, the presence of a match ensures that if the pushout complement exists then it is unique, hence a rewriting step can be deterministically performed. The theory of -adhesivity [2, 22] extends the core framework, ensuring that if the arrows of the rules are in a suitable class of monomorphisms then the benefits of adhesivity can be recovered [15, 16]. If only the left-hand side belongs to , the theory is still under development, as witnessed e.g. by [3]. However, despite the elegance and effectiveness, proving that a given category satisfies the conditions for being -adhesive can be a daunting task. For this reason, sufficient criteria have been provided for the core framework, e.g. that every elementary topos is adhesive [24], as well as for the extended one of -adhesivity [11]. For some structures such as hypergraphs with equivalence in [4], the question of their -adhesivity has not yet been settled.
E-graphs (shortly, EGGs) are an up-and-coming formalism for program optimisation and synthesis via a compact representation and efficient implementation of equality saturation. Albeit a classical data structure [14], EGGs received new impulse after the seminal [31] and developed a thriving community, as witnessed by the official website [29]. The key idea of rewriting-based program optimisation is to perform the manipulation of a syntactical description of a program, replacing some of its components in such a way that the semantics is preserved while the computational costs of its actual execution are improved. Instead of directly removing sub-programs, EGGs just add the new components and link them to the older ones via the equivalence relation, until an optimal program is reached and extracted.
EGGs can be concisely defined as term graphs equipped with a notion of equivalence on nodes that is closed under the operators of a signature [14, Section 4.2]. In the presentation of term graphs via string diagrams [11], EGGs are (hyper)trees whose edges are labelled by operators and with the possibility of sharing subtrees, with an additional equivalence relation on nodes that is closed under composition. In plain words and using a toy example: if and are two constants such that , then for any unary operator .
Building on the criterion developed in [11], this work proves that both hypergraphs with equivalence and EGGs form an -adhesive category for a suitable choice of . The advantages from this characterisation are two-fold. On the one side, we put the benefits per se of a formal presentation, making precise the properties of the data structure. On the other side, describing the optimisation steps via the DPO approach offers the tools for modelling their parallel and concurrent execution and for proving their confluence and termination.
Synopsis.
The paper has the following structure. In Section 2 we briefly recall the theory of -adhesive categories and of kernel pairs. In Section 3 we present the graphical structures of our interest, (labelled) hypergraphs and term graphs, and we provide a functorial characterisation, which allows for proving their adhesivity properties. This is expanded in Section 4 for proving the -adhesivity of hypergraphs and term graphs with equivalence and in Section 5 of their variants where equivalences are closed with respect to operator application, thus subsuming EGGs. In Section 6 we put the machinery at work, showing how the optimisation steps can be rephrased as the application of term graph rewriting rules. Finally, in Section 7 we draw our conclusions, hint at future endeavours and offer some brief remarks on related works. All the proofs can be found in the full version of the paper [6].
2 Facts about -adhesive categories and kernel pairs
We open this background section by fixing some notation. Given a category X we do not distinguish notationally between X and its class of objects, so “” means that is an object of X. We let , and denote the class of all arrows, monos and regular monos of X, respectively. Given an object , we denote by the unique arrow from an initial object into and by that unique arrow from into a terminal one. We will also use the notation to denote that an arrow is a regular epi.
2.1 -adhesivity
The key property of -adhesive categories is the Van Kampen condition [8, 23, 25], and for defining it we need some notions. Let X be a category. A subclass of is said to be
- 
 
stable under pushouts (pullbacks) if for every pushout (pullback) square as the one below, if () then (); 
- 
 
closed under composition if implies whenever and are composable; 
- 
 
closed under decomposition, or left-cancellable, if whenever and belong to , then . 
Definition 2.1.
Let be a class of arrows in a category X and consider the cube below.
We say that the bottom square is an -Van Kampen square if
- 
1. 
it is a pushout square; 
- 
2. 
whenever the cube above has pullbacks as back and left faces and the vertical arrows belong to , then its top face is a pushout if and only if the front and right faces are pullbacks. 
Pushout squares that enjoy only the “if” half of item (2) above are called -stable. A -Van Kampen square is called Van Kampen and a -stable square stable.
We can now define -adhesive categories.
Definition 2.2 ([2, 15, 16, 25, 22]).
Let X be a category and a subclass of including all isos, closed under composition, decomposition, and stable under pullbacks and pushouts. The category X is said to be -adhesive if
- 
1. 
it has -pullbacks, i.e. pullbacks along arrows of ; 
- 
2. 
it has -pushouts, i.e. pushouts along arrows of ; 
- 
3. 
-pushouts are -Van Kampen squares. 
A category X is said to be strictly -adhesive if -pushouts are Van Kampen. We write to denote that an arrow belongs to .
Remark 2.3.
-adhesivity is well-behaved with respect to the construction of (co-)slice and functor categories [26], as well with respect to subcategories, as shown by the following properties, taken from [17, Thm. 4.15], [25, Prop. 3.5] and [11, Thm. 2.12].
Proposition 2.4.
Let X be an (strict) -adhesive category. Then the following hold
- 
1. 
if Y is an (strict) -adhesive category, a functor preserving -pushouts and one preserving -pullbacks, then is (strictly) -adhesive, where 
- 
2. 
for every object the categories and are, respectively, (strictly) -adhesive and (strictly) -adhesive, where 
- 
3. 
for every small category Y, the category of functors is (strictly) -adhesive, where ; 
- 
4. 
if Y is a full subcategory of X closed under pullbacks and -pushouts, then Y is (strictly) -adhesive for every class of arrows of Y contained in that is stable under pullbacks and pushouts, contains all isos, and is closed under composition and decomposition. 
We briefly list some examples of -adhesive categories.
Example 2.5.
is adhesive, and, more generally, every topos is adhesive [24]. By the closure properties above, every presheaf is adhesive, thus the category is adhesive where is the two objects category with two morphisms . Similarly, various categories of hypergraphs can be shown to be adhesive, such as term graphs and hierarchical graphs [11]. Note that the category of simple graphs, i.e. graphs without parallel edges, is -adhesive [5] but not quasiadhesive.
We can state some useful properties of -adhesive category (see, for instance, [17, Thm. 4.26] or [2, Fact 2.6]).
Proposition 2.6.
Let X be an -adhesive category. Then the following hold
- 
1. 
every -pushout square is also a pullback; 
- 
2. 
every arrow in is a regular mono. 
2.2 Some properties of kernel pairs and regular epimorphisms
In this section we recall the definition and some properties of kernel pairs.
Definition 2.7.
A kernel pair for an arrow is an object together with two arrows , denoted as , such that the square below is a pullback.
Remark 2.8.
If is a a kernel pair for and a product of with itself exists, then the canonical arrow is a mono.
Remark 2.9.
An arrow is a mono if and only if it admits as a kernel pair.
The previous remarks allow us to prove the following result.
Proposition 2.10.
Let be an arrow and a mono. If is a kernel pair for , then it is also a kernel pair for .
Regular epis are particular well-behaved with respect to their kernel pairs.
Proposition 2.11.
Let be a regular epi in a category X with a kernel pair . Then, is the coequalizer of and .
We conclude this section exploring some properties of kernel pairs in an -adhesive category. The results below are simple, yet they appear to be original.
Lemma 2.12.
Let and be arrows admitting kernel pairs and suppose that the solid part of the four squares below is given. If the leftmost square is commutative, then there is a unique arrow making the other three commutative.
Moreover, the following hold
- 
1. 
if is a mono then is a mono; 
- 
2. 
if the leftmost square is a pullback then the central two are pullbacks; 
- 
3. 
if is mono and the leftmost square is a pullback then the rightmost is a pullback. 
The previous result allows us to deduce the following lemma in an -adhesive context.
Proposition 2.13.
Let X be a strict -adhesive category with all pullbacks, and suppose that in the cube below the top face is an -pushout and all the vertical faces are pullbacks. Then the right square is a pushout.
Focusing on , we can prove a sharper result.
Lemma 2.14.
Suppose that in the commuting cube in the diagram below on the left is given, whose top face is a pushout, the left and bottom faces are pullbacks, and is an injection. Then the following hold
- 
1. 
the right face of the cube is a pullback; 
- 
2. 
the right square, made by the kernel pairs of the vertical arrows, is a pushout. 
3 Hypergraphical structures
In this section we briefly recall the notion of hypergraph. In order to do so, a pivotal role is played by the Kleene star monad , also known as list monad, sending a set to the free monoid on it [28, 30]. We recall some of its proprieties.
Proposition 3.1.
Let be a set and . Then the following facts hold
- 
1. 
there are arrows such that is a coproduct; 
- 
2. 
for every arrow , is the coproduct of the family ; 
- 
3. 
preserves all connected limits [9], in particular it preserves pullbacks and equalizers. 
Remark 3.2.
Preservation of pullbacks implies that sends monos to monos.
Remark 3.3.
Notice that can be canonically identified with , thus for every set the arrow induces a length function , which sends a word to its length.
3.1 The category of hypergraphs
We open this section with the definition of hypergraphs and we show how to label them with an algebraic signature.
Definition 3.4.
An hypergraph is a 4-uple made by two sets and , called respectively the sets of hyperedges and nodes, plus a pair of source and target arrows .
A hypergraph morphism is a pair of functions , such that the diagrams below are commutative.
We define Hyp to be the resulting category.
Let be the functor sending to the product . We can use it to present Hyp as a comma category.
Proposition 3.5.
Hyp is isomorphic to
Note that by hypothesis preserves pullbacks, while is continuous by definition, hence by Proposition 3.5 and the characterisation of monos in comma categories we can deduce the following result.
Corollary 3.6.
A morphism is a mono in Hyp if and only if both its components are injective functions.
Applying Proposition 2.4 we also get the next corollary (cfr. [17, Fact 4.17]).
Corollary 3.7.
Hyp is an adhesive category.
Again Proposition 3.5 allows us to deduce immediately the following.
Proposition 3.8.
The forgetful functor which sends a hypergraph to its set of nodes has a left adjoint .
Example 3.9.
Since the initial object of Set is the empty set, is the hypergraph which has as set of nodes, as set of hyperedges, and as source and target function.
Example 3.10.
We represent hypergraphs visually: dots denote nodes and boxes hyperedges. Should we be interested in their identity, we put a name near the corresponding dot or box. Sources and targets are represented by lines between dots and squares: the lines from the sources of a hyperedge comes from the left of the box, while the lines to the targets exit from the right of the box. Let us look at the picture below. It represent a hypergraph with sets and of nodes and edges, respectively, such that have no source and a pair of nodes
Remark 3.11.
It is worth to point out, as first noted in [7], that Hyp is equivalent to a category of presheaves. Indeed, consider the category H in which the set of objects is given by and arrows are given by the identities , and exactly arrows , where ranges from to . The functors corresponds exactly to hypergraphs: nodes correspond to the image of while the set of hyperedges with source of length and target of length corresponds to the image of (see [11]).
In particular, Remark 3.11 entails the following result.
Proposition 3.12.
Hyp has all limits and colimits.
3.1.1 Labelling hypergraph with an algebraic signature
Our interest for hypergraphs stems from their use as a graphical representation of algebraic terms. We thus need a way to label hyperedges with symbols taken from a signature.
Definition 3.13.
An algebraic signature is a pair given by a set of operations and an arity function . We define the hypergraph associated with taking as set of hyperedges, as set of nodes, so that is , as the source function and , which always picks the element , as target function. The category of algebraically labelled hypergraphs is the slice category .
Example 3.14.
Let be the signature with , where stands for any natural number and for any element in , both sets of constants, and . Then the hypergraph is depicted as the picture below.
Proposition 2.4 gives us immediately an adhesivity result for and a characterisation of monos in it.
Proposition 3.15.
Let be an algebraic signature. Then the following hold
- 
1. 
an arrow in is a mono if and only if and are injective; 
- 
2. 
is an adhesive category. 
Remark 3.16.
Let be a hypergraph, by definition we know that is the terminal object , so an arrow , is determined by a function making the two squares below commutative (cfr. Remark 3.3).
Now, consider a coprojection . By the second diagram above entails that factors via the inclusion of words of length , i.e. all hyperedges must have a single target vertex, that is for some .
has a forgetful functor which sends to ). Now, since for every set we can define as . It is straightforward to see that in this way we get a left adjoint to .
Proposition 3.17.
has a left adjoint .
We extend our graphical notation of hypergraphs to labeled ones putting the label of an hyperedge inside its corresponding square.
Example 3.18.
Consider again the signature of Example 3.14, then the hypergraph of Example 3.10 can be labeled by a morphism that is characterised by the image of the edges. If , , , and , we represent it visually by putting the labels of the edges in inside the boxes representing the edges of .
3.2 Term Graphs
Term graphs have been adopted as a convenient way to represent terms over a signature with an explicit sharing of sub-terms, and as such have been a convenient tool for an efficient implementation of term rewriting [27]. We elaborate here on the presentation given in [11].
Definition 3.19.
Given an algebraic signature , we say that a labelled hypergraph is a term graph if is a mono. We define to be the full subcategory of given by term graphs and denote by the inclusion. Restricting we get a forgetful functor .
Remark 3.20.
By Remark 3.16, we know that if is a term graph then , where is the coprojection of into . Notice that since is a mono then is a mono.
Example 3.21.
The labelled hypergraph of Example 3.18 is a term graph.
We now examine some properties of , in order to study its adhesivity properties. We begin noticing that, for every set , has no hyperedges and so is a term graph. This yields at once the following.
Proposition 3.22.
The forgetful functor has a left adjoint .
We can list some other categorical properties of (see [11, Sec. 5]).
Proposition 3.23.
Let be an algebraic signature. Then the following hold
- 
1. 
if is a mono from to in and the latter is in , then also the former is in 
- 
2. 
has equalizers, binary products and pullbacks and they are created by . 
Remark 3.24.
in general does not have terminal objects. Since preserves limits, if a terminal object exists it must have the singleton as set of nodes, therefore the set of hyperedges must be empty or a singleton. Hence, for a counterexample, it suffices to take a signature with two operations and , both of arity . is not an adhesive category, either. In particular, as noted in e.g. [11], it does not have pushouts along all monos.
Definition 3.25.
Let be a term graph. A input node is an element of not in the image of . A morphism between and in , is said to preserve input nodes if sends input nodes to input nodes.
Preservation of input nodes characterizes regular monos in .
Proposition 3.26.
Let be a mono between two term graphs and . Then it is a regular mono if and only if it preserves the input nodes.
Lemma 3.27.
Consider three term graphs , and . Given , , if is a regular mono, then its pushout in along is a term graph.
Propositions 2.4 and 3.15, Proposition 3.26 and Lemma 3.27 allow us to recover the following result, previously proved by direct computation in [12, Thm. 4.2] (see also [11, Cor. 5.15] for the details of the argument presented here).
Corollary 3.28.
The category is quasiadhesive.
4 Adding equivalences to hypergraphical structures
The use of hypergraphs equipped with an equivalence relation over their nodes has been argued as a convenient way to express concurrency in the DPO approach to rewriting [4]. This section presents the framework by means of adhesive categories, including also its version for term graphs, as a stepping stone towards an analogous characterisation for e-graphs.
4.1 Hypergraphs with equivalence
Let us start with the case of general hypergraphs. These were introduced in [4], even if no general consideration about their structure as a category was proved, and adhesivity, which is the main focus here, was yet to be presented to the world.
Definition 4.1.
A hypergraph with equivalence is a 6-tuple such that is a hypergraph, is a set and is a surjection called quotient map. A morphism is a triple such that the following diagrams commute
The category of hypergraphs with equivalence and their morphisms is denoted EqHyp.
Remark 4.2.
Notice that in the classes of surjections, epis and regular epis coincide.
Remark 4.3.
Morphisms of hypergraphs with equivalences are uniquely determined by the first two components. That is, if and are two morphisms , then we have Since is epi, we obtain .
Forgetting the quotient part yields a functor sending a hypergraph with equivalence to . We now explore some of its properties to deduce information on the structure of EqHyp.
Proposition 4.4.
Consider the forgetful functor . Then the following hold
- 
1. 
is faithful; 
- 
2. 
has a left adjoint; 
- 
3. 
has a right adjoint. 
Corollary 4.5.
The functor preserves limits and colimits.
From the previous results we get the following characterization of monos in EqHyp.
Corollary 4.6.
An arrow in EqHyp is a mono if and only if is a mono in Hyp.
Now, we can consider the forgetful functor obtained by composing and . By Proposition 3.8 and the second point of Proposition 4.4 we get the following.
Corollary 4.7.
has a left adjoint .
Notice that there is another functor sending to , and a morphism to . We exploit it to compute limits and colimits in EqHyp.
Lemma 4.8.
Consider a diagram and let be the image of an object . Then the following hold
- 
1. 
has a colimit, which is preserved by ; 
- 
2. 
consider a cone limiting for and let be one for , then has a limit and there is a mono such that the diagram below commutes for every . 
Corollary 4.9.
An arrow in EqHyp is a regular mono if and only if all its components are injective functions.
We have now all the ingredients to study the adhesivity properties of EqHyp. As a first step we need to introduce a class of monos.
Definition 4.10.
We define as the class of regular monos in EqHyp such that the square below is a pullback
Now, we show that is a suitable class for adhesivity.
Lemma 4.11.
The class contains all isomorphisms, it is closed under composition, decomposition and it is stable under pullbacks and pushouts.
Finally, we show the key lemma for -adhesivity of EqHyp.
Lemma 4.12.
In EqHyp, -pushouts are stable.
Example 4.13.
It is noteworthy to show that pushouts along regular monos are not stable. Consider e.g. the cube below: the vertices are just graphs without edges, and in the graphs themselves the equivalence classes are denoted by encircling nodes with a dotted line. All the arrows are regular monos. It is immediate to see that the bottom face is a pushout and the side faces are pullbacks. Unfortunately, the top face fails to be a pushout.
Having proved stability of -pushouts, we now turn to prove that they are Van Kampen with respect to regular monos.
Lemma 4.14.
In EqHyp, pushouts along arrows in are -Van Kampen.
Corollary 4.15.
EqHyp is -adhesive.
4.2 Term graphs with equivalence
We are now going to generalize the work done in the previous section equipping term graphs with equivalence relation. First of all we need a notion of labelling for EqHyp.
Definition 4.16.
Let be an algebraic signature and the hypergraph associated to it. A labelled hypergraph with equivalence is a pair where is an object of EqHyp and a morphism of Hyp. A morphism of labelled hypergraphs with equivalence between and is an arrow such that .
We denote the resulting category by .
Let be an object of : since has a right adjoint by Proposition 4.4, corresponds to the arrow . It is immediate to see that this correspondence extends to an equivalence with the slice over .
Proposition 4.17.
is equivalent to .
Let be the functor forgetting the labelling and the class of morphisms in whose image in EqHyp is in . From Lemma 4.8, the second point of Propositions 2.4 and 4.15, we can deduce the following.
Proposition 4.18.
Consider the forgetful functor . Then the following hold
- 
1. 
has all colimits and all connected limits, which are created by ; 
- 
2. 
is -adhesive. 
Using Corollaries 4.6 and 4.9 we immediately get the following result.
Corollary 4.19.
Let be an arrow in . Then the following hold
- 
1. 
is mono if and only if and are injective; 
- 
2. 
is a regular mono if and only if , and are injective. 
We can now easily define term graphs with equivalence.
Definition 4.20.
Let be an algebraic signature. An object of is a term graph with equivalence if is a term graph. We denote by the full subcategory of so defined and by the corresponding inclusion functor.
Remark 4.21.
Let the forgetful functor lifting . Notice that, by definition, is in amounts to say that is in . Thus there exists a functor as in the diagram below.
Remark 4.22.
Notice that, by Corollary 4.5 and Proposition 4.18, the functor preserves all connected limits and all colimits.
The previous remark allows us to prove an analog of Proposition 3.23.
Proposition 4.23.
has equalizers, binary products and pullbacks and they are created by .
Let now be the class of morphism in whose image through is in and whose image through is a regular mono in . By Propositions 3.26 and 4.19, we have that a morphism is in if and only if all of its components are injections and the square below is a pullback.
In particular, by Corollary 3.28 and Lemma 4.12, contains all isomorphisms, is closed under composition and decomposition and stable under pushout and pullbacks.
The following proposition is now an easy corollary of Lemma 3.27, Proposition 4.18, and Remark 4.21.
Proposition 4.24.
has all -pushouts, which are created by .
Corollary 4.25.
is -adhesive.
5 EGGs
The previous section proved some adhesivity property for the categories EqHyp and . We extend these results to encompass equivalence classes which are closed under the target arrow, i.e. under operator composition for term graphs, thus precisely capturing EGGs.
5.1 E-hypergraphs
We start introducing the notion of e-hypergraphs, hypergraphs equipped with an equivalence relation that is closed under the target arrow: in other words, whenever the relation identifies the source of two hyperedges, it identifies their targets too.
Definition 5.1.
Let be a hypergraph with equivalence and a kernel pair for . We will say that is an e-hypergraph if .
We will denote by the full subcategory of EqHyp whose objects are e-hypergraphs, and by the associated inclusion functor.
Example 5.2.
Consider the hypergraph of Example 3.10 and consider as quotient the identity . Then the kernel pair of concide with the kernel pair of , which is empty. Thus is, trivially, an e-hypergraph
A first result that we need is that limits in are computed as in EqHyp.
Lemma 5.3.
has all limits and creates them.
Corollary 5.4.
If an arrow in is a regular mono in then is a regular mono in Hyp.
We can now turn to pushouts.
Lemma 5.5.
Suppose that the square below is a pushout in EqHyp and that is a mono in . If , and are e-hypergraphs, then is is an e-hypergraph.
Let now be the class of arrows in that are sent to by . By definition and Lemma 4.11 we have that such class is closed under composition and decomposition, it contains all isomorphisms and it is stable under pullbacks. Using Lemmas 4.11 and 5.5 we can further deduce that it is stable under pushouts. Then Propositions 2.4 and 4.15 yield the following.
Corollary 5.6.
is -adhesive.
5.2 E-term graphs, a.k.a. EGGs
We now turn our attention to the labelled context.
Definition 5.7.
We say that an object of is a labelled e-hypergraph if is an e-hypergraph. We define the category as the full subcategory of given by labelled e-hypergraphs. We denote by the corresponding inclusion functor .
To prove some adhesivity property of , we begin with the following elementary, yet useful observation.
Remark 5.8.
Given a signature , the hypergraph is an object of . Indeed, under the identification of with , the kernel of , is given by equipped with the two projections. On the other hand, both and are the function constant in .
Remark 5.8 now implies at once the following result.
Proposition 5.9.
Let be an algebraic signature. Then the following hold
- 
1. 
is equivalent to ; 
- 
2. 
there exists a functor forgetting the labeling which creates all colimits, pullbacks and equalizers. 
Let be the class of morphisms in whose image in lies in . Notice that is also the class of arrows whose image through is in . By Proposition 2.4 we get the following result.
Corollary 5.10.
is -adhesive.
We turn now to term graphs with equivalence.
Definition 5.11.
Given a signature , we say that an object of is an e-term graph if is an e-hypergraph. We define the category as the full subcategory of given by e-term graphs and denote by the corresponding inclusion.
Remark 5.12.
By definition we also have an inclusion functor .
Remark 5.13.
Now that we have put all the structures of this work in place, it is worthwhile to give a visual map of all the categories that we have discussed/introduced and of the relationships between them. We will use the curved arrows to denote full and faithful inclusions.
We can now prove our last three results.
Proposition 5.14.
has equalizers, binary products and pullbacks and they are created by .
Let now be the class of morphisms of which are sent by to the class . Then Lemmas 5.5 and 4.24 allow us to deduce the following result.
Proposition 5.15.
has -pushouts, which are created by .
Reasoning as in the previous sections, Propositions 5.14 and 5.15 now give us the following.
Corollary 5.16.
is -adhesive.
6 Pros and cons of adhesive rewriting
The previous sections have shown how hypergraphs and term graphs with equivalence can be described as suitable -adhesive categories. The same fact holds for their sub-categories where the equivalence is closed with respect to operator composition, and this allows to model EGGs, as originally presented in [31].
Sharing.
Terms are trees, thus different term graphs may represent the same term, up-to the sharing of sub-terms. Consider e.g. a constant and a binary operator : the term admits a few different representations as a term graph with equivalence, as shown below
The left-most and the right-most images represent just ordinary term graphs, the middle one is a term graph with equivalence. As such, they are all objects of . However, note that the right-most image is not an object of : constants have no input, and nodes that are targets of edges with the same label and whose source is the empty word must be equivalent and possibly coincide, as in the middle and in the left-most image, respectively.
In general, for (acyclic) term graphs, a maximally and a minimally shared representation exist, and for our toy example are the left-most and the right-most diagram above, respectively: it is a standard result for term graph rewriting, see e.g. [1]. These two representations can be interpreted as the final and the initial object of a suitable comma category. respectively. The same properties carry on for term graphs with equivalence and for EGGs. However, while in the former case the two representations are the same of those for ordinary term graphs, in the latter case the minimally shared representation is now the middle diagram.
Rewriting.
The theory of -adhesivity ensures that if the rules are spans of arrows in , then we can lift the standard properties of the DPO approach that hold in the category of graphs. However, as we recalled in the introduction, instead of removing sub-terms, the EGG approach chooses to just add new terms and link them to the older ones via the equivalence relation [14]. So, the corresponding DPO rules are spans , where the first component is the identity, thus in , while the second component may not belong to .
Consider e.g. an EGG rule such , from the introductory example in [31]. Variables in a term graph are represented as nodes that do not occur among the targets of an edge, thus the rule can be modelled as the DPO rule below, concisely given by the arrow
In this case is the minimally shared EGG corresponding to the term , thus the rule could be applied to the two EGGs depicted above with a match that is injective on equivalence classes. In general, if the term graph underlying the EGG to whom the DPO rule is applied is acyclic, the same property holds for the EGG obtained as the result of the rule application. The right-hand side is a regular mono, though, hence it is not an arrow in . The asymmetry between left-hand and right-hand sides falls in the current research about left-linear rules for adhesive categories, as pursued e.g. in [3].
Application conditions.
As argued above, the DPO rules that are suitable for the EGG approach have identities as left-hand side, thus they belong to any choice of . However, this would allow for the repeated application of the same rule, hence the possibility to keep on performing the same rewriting step. This is forbidden by using rules with negative application conditions, given as the usual span plus an additional arrow , such that a match is admissible if it cannot be factorised through [21]. For EGGs and rules , it suffices to choose as the right-hand side itself. The theory of -adhesivity carries on for rules with negative application conditions, see [15, 16].
7 Conclusions and further and related works
The aim of our paper was to extend the theory of -adhesive categories in order to include EGGs, a formalism for program optimisation. To do so, we revisited the notions of hyper-graphs and term graphs with equivalence, proving that they are -adhesive categories, and we extended these results in order to prove the same property for EGGs as term graphs with equivalence satisfying a suitable closure constraint. Summing up, we proved that EGGs are objects of an -adhesive category and that optimisation steps are obtained via DPO rules (possibly with negative application conditions) whose left-hand side is in , and that allows for exploiting the properties of the -adhesive framework.
Future works.
Our result on opens a few threads of research. The first is to check how the -adhesivity of EGGs can be pushed to model their rewriting via the double-pushout (DPO) approach. We have seen that the rules adopted in the literature of EGGs appears to be spans whose left-hand side is an identity and right-hand side is a regular mono (possibly with negative application conditions), and as such they fit the mould of rewriting on left-linear rewriting in -adhesive categories. However, it still needs to be shown how parallelism and causality, the key features for DPO rewriting on -adhesive categories, can be exploited in the context of implementing the EGGs updates. Moreover, extensions of the EGGs formalism could be suggested by the adhesive machinery we developed. In fact, most of the results presented here for hypergraphs can be generalised to hierarchical hypergraphs, that is, hypergraphs with a hierarchy (a partial order) among edges [20, 11]. The additional structure is useful for modelling properties such as encapsulation and sandboxing, and it seems worthwhile to check the expressiveness and applications of hierarchical EGGS.
Related works.
Despite the interest they have ben raising as an efficient data structure, we are not aware of any attempt to provide an algebraic characterisation of EGGs, the only exception being [19]. We leave for future work an in-depth comparison among the two proposals, which appear to be related yet quite different. In fact, for both proposals the key intuition is to use string diagrams to represent term graphs. We adopted a more down-to-earth approach by equipping concretely the nodes of a term graph with an equivalence relation, thus directly corresponding to the original presentation [14], at the same time extending and generalising it [4] in the contemporary jargon of adhesive categories. The route chosen in [19] is to consider term graphs as arrows of a symmetric monoidal category, and equipping them with an enrichment over semi-lattices, the resulting formalism being reminiscent of hierarchical hypergraphs. Thus, the solution in [19] is more general than ours since it can be lifted to term graphs defined over categories other than . At the same time, we both consider the DPO approach for rewriting, even if only our solution guarantees that the resulting category is actually -adhesive, thus allowing to exploit all the features of the framework as they hold for DPO graph transformation.
References
- [1] Zena M. Ariola, Jan Willem Klop, and Detlef Plump. Bisimilarity in term graph rewriting. Information and Computation, 156(1-2):2–24, 2000. doi:10.1006/INCO.1999.2824.
- [2] Guilherme Grochau Azzi, Andrea Corradini, and Leila Ribeiro. On the essence and initiality of conflicts in -adhesive transformation systems. Journal of Logical and Algebraic Methods in Programming, 109:100482, 2019. doi:10.1016/J.JLAMP.2019.100482.
- [3] Paolo Baldan, Davide Castelnovo, Andrea Corradini, and Fabio Gadducci. Left-linear rewriting in adhesive categories. In Rupak Majumdar and Alexandra Silva, editors, CONCUR 2024, volume 311 of LIPIcs, pages 11:1–11:24. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2024. doi:10.4230/LIPICS.CONCUR.2024.11.
- [4] Paolo Baldan, Fabio Gadducci, and Ugo Montanari. Concurrent rewriting for graphs with equivalences. In Christel Baier and Holger Hermanns, editors, CONCUR 2006, volume 4137 of LNCS, pages 279–294. Springer, 2006. doi:10.1007/11817949_19.
- [5] Nicolas Behr, Russ Harmer, and Jean Krivine. Fundamentals of compositional rewriting theory. Journal of Logical and Algebraic Methods in Programming, 135:100893, 2023. doi:10.1016/J.JLAMP.2023.100893.
- [6] Roberto Biondo, Davide Castelnovo, and Fabio Gadducci. EGGs are adhesive! CoRR, abs/2503.13678, 2025. doi:10.48550/arXiv.2503.13678.
- [7] F. Bonchi, F. Gadducci, A. Kissinger, P. Sobociński, and F. Zanasi. String diagram rewrite theory I: Rewriting with Frobenius structure. Journal of the ACM, 69(2):1–58, 2022. doi:10.1145/3502719.
- [8] Ronald Brown and George Janelidze. Van Kampen theorems for categories of covering morphisms in lextensive categories. Journal of Pure and Applied Algebra, 119(3):255–263, 1997.
- [9] Aurelio Carboni and Peter Johnstone. Connected limits, familial representability and Artin glueing. Mathematical Structures in Computer Science, 5(4):441–459, 1995. doi:10.1017/S0960129500001183.
- [10] D. Castelnovo. Fuzzy algebraic theories and -adhesive categories. PhD thesis, University of Udine, 2023.
- [11] Davide Castelnovo, Fabio Gadducci, and Marino Miculan. A simple criterion for ,-adhesivity. Theoretical Computer Science, 982:114280, 2024.
- [12] Andrea Corradini and Fabio Gadducci. On term graphs as an adhesive category. In Maribel Fernández, editor, TERMGRAPH 2004, volume 127(5) of ENTCS, pages 43–56. Elsevier, 2004. doi:10.1016/J.ENTCS.2005.02.014.
- [13] Andrea Corradini, Ugo Montanari, Francesca Rossi, Hartmut Ehrig, Reiko Heckel, and Michael Löwe. Algebraic approaches to graph transformation - part I: basic concepts and double pushout approach. In Grzegorz Rozenberg, editor, Handbook of Graph Grammars and Computing by Graph Transformations, Volume 1: Foundations, pages 163–246. World Scientific, 1997.
- [14] David Detlefs, Greg Nelson, and James B. Saxe. Simplify: a theorem prover for program checking. Journal of the ACM, 52(3):365–473, 2005. doi:10.1145/1066100.1066102.
- [15] H. Ehrig, U. Golas, A. Habel, L. Lambers, and F. Orejas. -adhesive transformation systems with nested application conditions. Part 2: Embedding, critical pairs and local confluence. Fundamenta Informaticae, 118(1-2):35–63, 2012. doi:10.3233/FI-2012-705.
- [16] H. Ehrig, U. Golas, A. Habel, L. Lambers, and F. Orejas. -adhesive transformation systems with nested application conditions. Part 1: Parallelism, concurrency and amalgamation. Mathematical Structures in Computer Science, 24(4):240406, 2014. doi:10.1017/S0960129512000357.
- [17] Harmut Ehrig, Karsten Ehrig, Ulrike Prange, and Gabriele Taentzer. Fundamentals of Algebraic Graph Transformation. Springer, 2006. doi:10.1007/3-540-31188-2.
- [18] R. Garner and S. Lack. On the axioms for adhesive and quasiadhesive categories. Theory and Applications of Categories, 27(3):27–46, 2012.
- [19] Dan R. Ghica, Chris Barrett, and Aleksei Tiurin. Equivalence hypergraphs: E-graphs for monoidal theories. CoRR, abs/2406.15882, 2024. doi:10.48550/arXiv.2406.15882.
- [20] Dan R. Ghica and Fabio Zanasi. Hierarchical string diagrams and applications. CoRR, abs/2305.18945, 2023. doi:10.48550/arXiv.2305.18945.
- [21] Annegret Habel, Reiko Heckel, and Gabriele Taentzer. Graph grammars with negative application conditions. Fundamenta Informaticae, 26(3/4):287–313, 1996. doi:10.3233/FI-1996-263404.
- [22] T. Heindel. A category theoretical approach to the concurrent semantics of rewriting. PhD thesis, Universität Duisburg–Essen, 2009.
- [23] Peter T. Johnstone, Stephen Lack, and Pawel Sobocinski. Quasitoposes, quasiadhesive categories and Artin glueing. In Till Mossakowski, Ugo Montanari, and Magne Haveraaen, editors, CALCO 2007, volume 4624 of LNCS, pages 312–326. Springer, 2007. doi:10.1007/978-3-540-73859-6_21.
- [24] Stephen Lack and Pawel Sobocinski. Toposes are adhesive. In Andrea Corradini, Hartmut Ehrig, Ugo Montanari, Leila Ribeiro, and Grzegorz Rozenberg, editors, ICGT 2006, volume 4178 of LNCS, pages 184–198. Springer, 2006. doi:10.1007/11841883_14.
- [25] Sthephen Lack and Pawel Sobociński. Adhesive and quasiadhesive categories. RAIRO-Theoretical Informatics and Applications, 39(3):511–545, 2005. doi:10.1051/ITA:2005028.
- [26] Saunders Mac Lane. Categories for the working mathematician. Springer, 2013.
- [27] D. Plump. Term graph rewriting. In H. Ehrig, G. Engels, H-J Kreowski, and G. Rozenberg, editors, Handbook of Graph Grammars and Computing by Graph Transformation, Volume 2: Applications, Languages and Tools, pages 3–61. World Scientific, 1999.
- [28] J. Sakarovitch. Elements of automata theory. Cambridge University Press, 2009.
- [29] VV.AA. egg: e-graphs good. https://egraphs-good.github.io/.
- [30] Philip Wadler. Monads for functional programming. In Johan Jeuring and Erik Meijer, editors, Advanced Functional Programming, volume 925 of LNCS, pages 24–52. Springer, 1995. doi:10.1007/3-540-59451-5_2.
- [31] Max Willsey, Chandrakana Nandi, Yisu Remy Wang, Oliver Flatt, Zachary Tatlock, and Pavel Panchekha. egg: Fast and extensible equality saturation. In POPL 2021, volume 5 of PACMPL, pages 1–29. ACM, 2021. doi:10.1145/3434304.
