3.5. MinIR rewriting, operationally
For these reasons, our presentation does not adhere to DPO semantics and is purely operational rather than categorical. This section introduces the graph rewriting operations, i.e. the graph mutation operations that we will consider. It then presents some sufficient conditions for graph rewrites to preserve the minIR validity conditions of Definition 3.4, as well as a discussion of how more complex rewrites can be achieved by composition.
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.
All graph transformations that we consider operate through local graph rewrites, which we define 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 .
Define 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.
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.5). 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 . Recall that designates the type morphism on . We lift to be defined on ordered list of values by element-wise application of the map and define the interface of in as
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 .
Notice that in (2), we lifted the notation to ordered list types : we write if there is such that .
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 say that the subgraph implements the interface
Remark, though, that unlike operations, the same subgraph may implement more than one interface as a result of various choices of orderings and .
Importantly, a minIR subgraph is a graph but is in general not a valid minIR graph. A non-empty use-boundary corresponds to values without definitions in , and a non-empty set corresponds to linear values in without uses. We can however always construct a valid minIR graph from by adding two operations and in the root region, defined by
where denotes the empty string in . 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 If , then is not injective on and , and by the definition of , and . Otherwise, there are such that . The same value is used twice, which is only a valid minIR graph if and are not linear, thus proving Claim 1.
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 in . 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 along with a subgraph of that we will now call , to distinguish from .
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 then according to (1), the graph resulting from applying to can be expressed as the glueing
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. ↩︎