3.5. MinIR rewriting, operationally
The previous section proposed to view minIR rewrites as the result of a (DPO or SqPO) graph transformation. This yields valid rewriting semantics elegantly (and with little effort!). However, the conditions that must be imposed on the transformation to be valid, along with the fact that pushouts may not exist mean that the existence of a rewrite given a transformation rule and a match is not guaranteed.
In this section, we address this by considering a more restricted notion of minIR rewriting, for which the existence of the right-hand side of the rewrite is guaranteed. In addition, in place of the categorical presentation of the last section, we express the rewriting operation operationally, i.e. as data and a procedure on sets that translates directly into an algorithmic implementation.
We find that this rewrite definition is sufficient in practice. We conclude the section with an example of how more complex rewrites can be achieved by composition of simpler rewrites that can be expressed in this framework.
Graph glueings and rewrites #
Throughout, we consider graph glueings on disjoint vertex and (hyper)edge sets. To underline this, we will use the symbol to denote disjoint set unions.
As we will be working exclusively with vertex and edge sets in this section (as opposed to the objects in the indexing category), we will drop the bold typeface for sets, writing e.g. instead of for the set of vertices of a hypergraph.
Finally, all minIR graphs in this section are IO free.
We define local graph rewrites in terms of graph glueings. Consider first the case of two arbitrary graphs and , along with a relation . Let be the equivalence relation induced by , i.e. the smallest relation on that is reflexive, symmetric and transitive, and satisifes for all and ,
Then, we can define
- is the set of all equivalence classes of , and
 - for , is the equivalence class of that belongs to.
 
The glueing of and according to the glueing relation is given by the vertices and the edges
We write the glueing graph as .
In other words, the glueing is the disjoint union of the two graphs, with identification (and merging) of vertices that are related in .
This allows us to define a rewrite on a graph :
A rewrite on a graph is given by a tuple , with
- is a graph called the replacement graph,
 - is the vertex deletion set,
 - is the edge deletion set, and
 - is the glueing relation, a partial function that maps a subset of the deleted vertices of to vertices in the replacement graph.
 
The domain of definition is known as the boundary values of .
A graph rewrite per this definition can always be generated by a single pushout (SPO) transformation Löwe, 1991. 1991. Extended algebraic graph transformation. Retrieved from http://d-nb.info/910935696.
- define as the graph . Then the injection is the match morphism ;
 - the partial map maps a subset of to vertices in the replacement . By injectivity of the match morphism, it also defines a partial map .
 
We opted for SPO-like semantics in this definition, as they are the simplest to write in set-theoretic terms and coincide with DPO and SqPO in our restricted domain of interest.
The result of the rewrite is computed by gluing the right-hand side to the context subgraph of given by
The partial function is a special case of a glueing relation , and thus defines a glueing of with . The rewritten graph resulting from applying to is
An example of a graph rewrite is given in the next figure. This is equivalent to an SPO transformation with the graph induced by on the left-hand side, the graph on the right-hand side and the partial map given by .
Application of a graph rewrite. On the left, the original graph along with the replacement graph (grey box). On the right, the rewritten graph . Only the vertex has been deleted, as other vertices in are in the boundary (in orange). The (singleton) edge deletion set is red. The blue edge connects a vertex of to a boundary vertex, and is thus also present on the right-hand side. The purple edge, on the other hand, connects a vertex of to a non-boundary vertex of , and is thus deleted.
When there are no edges between and (purple in the example above), this definition corresponds to graph rewrites that can be produced using DPO transformations (see discussion in section 3.4). Otherwise, such edges are deleted.
The notions of graph glueing and graph rewrite can straightforwardly be lifted to hypergraphs and, by extension, to minIR graphs. Notice that in this case, values are glued together, not operations (the former were defined as the graph’s vertices, the latter as its hyperedges).
However, the glueing of two valid minIR graphs – and the result of applying a valid rewrite – may not be a valid minIR graph. Glueing two values of a linear type, for instance, is a sure way to introduce multiple uses (or definitions) of it. Thus, we must be careful to only consider glueings and rewrites of minIR graphs that preserve all the constraints we have imposed in Definition 3.4.
Ensuring rewrite validity: interfaces #
As a sufficient condition for valid minIR rewrites, we introduce minIR interfaces, a concept closely related to the “hypergraph with interfaces” construction of Bonchi, 2017. 2017. Confluence of Graph Rewriting with Interfaces or the supermaps of quantum causality Hefford, 2024. 2024. A Profunctorial Semantics for Quantum Supermaps. In Proceedings of the 39th Annual ACM/IEEE Symposium on Logic in Computer Science, July 2024. ACM, 1--15. doi: 10.1145/3661814.3662123. We eschew the presentation of holes as a slice category in favour of a definition that fits naturally within minIR and is sufficient for our purposes.
Let be a -typed minIR graph with data types and linear types . Consider type strings . We define the index sets
corresponding respectively to the set of all indices into and the subset of indices of linear types. For any , we denote by the type at position i in .
We define a partial order 1 on where and say that can be coerced into if there exists an index map such that
- types are preserved: , and
 -  is well-defined and bijective on the restriction to indices of linear
types
 
Let be a set of data types. An interface is a pair of type strings .
We say that an interface can be coerced into an interface , written , if and .
We can define the interface associated with an operation in a minIR graph by considering the values used and defined by . Calling the type morphism on and assuming to be an operation in with inputs and outputs, we define the interface of in as the pair of strings in
Similarly, we can assign interfaces to subgraphs of minIR graphs:
Consider a subset of values and operations and . Define the use and define boundary sets
The tuple of is called a minIR subgraph of if there exists a region of such that all boundary values of are in :
We write to indicate that is a minIR subgraph of .
Note that is exactly the set of inputs in the non-IO free minIR graph given by the subgraph of the minIR graph. is a superset of the outputs of : it includes all linear values in that do not have a use in , but also any non-linear value that has a use outside of .
Unlike interfaces, subgraph boundary values are not ordered. An ordering of is a string along with a bijective map
If there are strings and orderings of and
then we can set and in complete analogy to operations. We will write and for the strings and respectively. We say that the subgraph implements the interface
where the type morphism was extended element-wise to strings .
Remark, though, that unlike operations, the same subgraph may implement more than one interface as a result of various choices of orderings and .
As mentioned, the subgraph forms a non-IO free minIR graph. We can always construct an IO-free minIR graph from by adding two operations and in the root region respectively in and inputs-outputs, defined by
We call the resulting graph  an interface graph. It implements the
interface  if  implements . Calling to mind the illustrations of
section 3.3,  looks like one of the nested regions
within regiondef operations that we were considering.
MinIR operation rewrite #
Consider
- an operation in a minIR graph with values
 - an interface graph with values and its associated subgraph , such that implements an interface
 - the index maps and that define the generalisation (per Definition 3.10).
 
We can define a glueing relation
This is almost enough to define a rewrite that replaces the operation in with the values and operations of – the interface compatibility constraint that we have imposed ensures that the resulting minIR graph is valid. Unfortunately, is not a partial function as required by Definition 3.4.
This is resolved in the following proposition:
Let , and such that , as defined above. Then
i.e. the graph obtained by removing the operation from the glueing of and along , is a valid minIR graph.
There is a graph with values and a partial function such that the graph (5) is the graph , obtained from the rewrite
We call the rewrite of into .
The definition of the rewrite of into a graph behaves as one would expect – the only subtleties relate to handling non-linear (i.e. copyable) values at the boundary of the rewrite. The following example illustrates some of these edge cases.
Rewriting operation in the graph (top left) into the operations and of the graph (bottom left). Coloured dots indicate the index maps and from inputs of to inputs of , respectively from outputs of to outputs of .
When the index maps and are not injective (yellow and green dots), values are merged, resulting in multiple uses of the value (i.e. copies). This is why the index maps must be injective on linear values (dots in shades of blue). Value merging also happens when a value is used multiple times in (yellow and red dots). This will never happen with linear values (as they can never have more than one use in ), nor with any value definitions (the same value can never be defined more than once). Finally, values not in the image of or (purple dot) are discarded. This case is also excluded for linear values by requiring surjectivity.
Proof
We start this proof with the explicit construction of and . Define as the smallest equivalence relation such that
Then we define , the graph obtained by glueing together values within the same equivalence class of .
Claim 1: is a valid minIR graph.
Claim 1 follows from the observation that only values of non-linear types are glued together. If , then either or there exist such that
Define as the subgraph obtained from by removing the operations . Let be the set of values of (and of ). Writing for the equivalence class of that belongs to, we can define as:
Claim 2: is a partial function .
In other words, for all , then . Let and be values in . First of all, for all , otherwise is not acyclic. So either , or , but not both.
The simpler case: if , then there exists such that . Furthermore is unique because by minIR definition, has a unique definition in . It follows from (4) that and hence .
Otherwise, there exists and such that and as well as . By definition of , we have , and thus
proving Claim 2.
Claim 3: is given by .
It follows directly from our construction of and that the equivalence classes of (the smallest equivalence relation closure of) is equal to the equivalence classes of (the smallest equivalence relation closure of) . The claim follows by Definition 3.8 and the definition of .
And finally, Claim 4: is a valid minIR graph.
Per Definition 3.4, We must check four properties: (i) every value is defined exactly once, (ii) every linear value is used exactly once, (iii) the graph is acyclic, and (iv) every region has (at most) one parent.
(iii) follows from the fact that and are acyclic and a single operation in is replaced: any cycle across and would also be a cycle in by replacing the subpath in with . (iv) follows from the fact that and are in the root region of , by definition of interface implementation. (i): removing from removes the unique definitions of all values that are targets of . Each such value is glued to a unique value in – the new and unique definition of in (ii) follows from the same argument as in (i), but relying on injectivity of on linear values to establish uniqueness.
Arbitrary minIR rewrites #
We have so far defined rewrites of single operations into graphs . We can generalise these rewrites to rewrite subgraphs , provided the minIR subgraphs satisfy some constraints. We require for this a notion of convexity, as discussed in Bonchi, 2022. 2022. String diagram rewrite theory II: Rewriting with symmetric monoidal structure. Mathematical Structures in Computer Science 32, 4 (April 2022, 511--541). doi: 10.1017/s0960129522000317.
As usual, let us consider a minIR graph with values , linear values , edges , the incidence maps and as well as their inverses and . Consider further a subgraph of that we will now call , to distinguish from .
Let us further define the partial morphism that maps a value to the parent of the region of .
A minIR subgraph is convex if the following conditions hold:
- for all , any path along from to contains only vertices in ,
 - parent-child relations are contained within the subgraph, i.e.
 
Define the sets of boundary values and , as in (2); then fix the boundary orderings and as in (3). The subgraph implements the interface
Consider an interface graph that implements such that . Instead of defining a gluing relation from values of an operation to values of , we replace the interface with . This generalises the definition of from (4) to a glueing defined as
With the set of boundary operations defined as2
we are able to define minIR rewrites in their most general form.
Let and such that and is convex, as defined above. Then,
i.e. the graph obtained by removing the values and operations from the glueing of and along , is a valid minIR graph.
There is a graph with values and a partial function such that the graph (8) is the graph , obtained from the rewrite
We call the rewrite of into .
Proof
Consider an operation that implements . We can define the interface graph given by three operations , and . Its associated subgraph only includes . Let be the glueing relation
Consider the rewrite . If we write for the subgraph of given by
Our claim is that is a valid minIR graph.
The graph (8) is then obtained by applying the rewrite as given by (6) to . Defining the rewrite as the composition of followed by , the result follows from our claim and Proposition 3.5.
We now prove the claim, by showing the four properties of minIR graphs as per Definition 3.4. Property i) requires showing that every value is defined exactly once. As is obtained by removing values and operations from a valid minIR graph , no value in can be defined more than once. A value that is not defined in must be in the boundary of . By the boundary definitions of (2), cannot be in and thus must be in . It follows by the definition of the glueing that in , will be in the definitions of : . The glueing is bijective between the values of and and thus we can conclude that has a unique definition in .
The same argument applies to property ii). Property iii) follows from the convexity requirement of . Finally, property iv) (every region has at most one parent) follows from two observations. First, by convexity of , no deleted value or operation could be the parent of any value not in , and thus the relation is well-defined on : . Secondly, all new values and operations added to the boundary region of are from the root region of , and thus do not have a parent, ensuring that parent uniqueness is preserved.
This simple and limited graph transformation framework captures a remarkably large set of minIR program transformations. It may seem at first that the restriction to boundary values within a single region of Definition 3.11, as well as the convexity requirements of Definition 3.12 represent significant limitations on the expressivity of the rewrites. In practice, however, the semantics of minIR operations can be used to decompose more complex rewrites into a sequence of simple rewrites to which Proposition 3.6 applies.
Consider minIR graphs with a type system that includes regiondef and call
operations as discussed in examples of the previous section – respectively
defining a code block by a nested region and redirecting control flow to a code
block defined using a regiondef. Then all constraints that we impose on
rewriting can be effectively side-stepped using the region outlining and
value hoisting transformations.
Region outlining moves a valid minIR subgraph into its own separate region,
and replaces the hole left by the subgraph in the computation by a call
operation to the newly outlined region.
Value hoisting moves a value definition within a region to its parent region and passes the value down to the nested region through an additional input. In case of linear values, we can similarly hoist the unique use of the value to the parent region.
Using these transformations, non-convex subgraphs can always be made convex by taking the convex hull and outlining any parts within it that are not part of the subgraph. Outlined regions can then be passed as additional inputs to the subgraph. Step 1 of the figure below illustrates this transformation. Similarly, a subgraph that includes operations without their parent can be extended to cover the entire region and its parent, outlining any parts of the region that are not part of the subgraph.
Finally, whenever a boundary value belongs to a region that is not the top level region of the subgraph3, we can repeatedley hoist to its parent region until it is in the top level region. The value is then recursively passed as argument to descendant regions until the region that it is required in. Subgraphs can thus always be transformed to only have input and output boundary values at the top level region. Step 2 of the figure below illustrates this transformation.
A non-convex minIR graph rewrite, obtained by decomposition into valid convex rewrites, using outlining and hoisting. For simplicity, regiondef operations were made implicit and represented by nested boxes: a region within an operation corresponds to a region definition that is passed as an argument to the operation. Edge colours correspond to value types. Step 1 outlines the ... operations into a dedicated region, which step 2 hoists outside of the region being rewritten. Step 3 and 4 together correspond to a minIR sugraph rewrite. They have been split into two steps following the proof strategy. Step 4 is an instance of a minIR operation rewrite.
To be precise, is a partial order on the type strings up to isomorphism. ↩︎
The set operations and are again understood to apply to the unordered set of elements contained in the lists and . ↩︎
We can always extend a subgraph to contain more ancestor regions, until there is indeed a unique top-level region in the subgraph. ↩︎