MinIR rewriting, operationally

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 \sqcup 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 G1=(V1,E2)G_1 = (V_1, E_2) and G2=(V2,E2)G_2 = (V_2, E_2), along with a relation μ V1×V2\mu\ \subseteq V_1 \times V_2. Let μ (V1V2)2\sim_\mu \ \subseteq (V_1 \sqcup V_2)^2 be the equivalence relation induced by μ\mu, i.e. the smallest relation on V1V2V_1 \sqcup V_2 that is reflexive, symmetric and transitive, and satisifes for all v1V1v_1 \in V_1 and v2V2v_2 \in V_2,

(v1,v2)μv1μv2.(v_1, v_2) \in \mu \Rightarrow v_1 \sim_\mu v_2.

Then, we can define

  • V=(V1V2)/μV = (V_1 \sqcup V_2)/\sim_\mu is the set of all equivalence classes of μ\sim_\mu, and
  • for vV1V2v \in V_1 \sqcup V_2, αμ(v)V\alpha_\mu(v) \in V is the equivalence class of μ\sim_\mu that vv belongs to.
Definition 3.8Graph glueing

The glueing of G1G_1 and G2G_2 according to the glueing relation μ\mu is given by the vertices V=(V1V2)/μV = (V_1 \sqcup V_2)/\sim_\mu and the edges

E={(αμ(u),αμ(v))(u,v)E1E2}V2.E = \{(\alpha_\mu(u), \alpha_\mu(v)) \mid (u,v) \in E_1 \sqcup E_2 \} \subseteq V^2.

We write the glueing graph as (G1G2)/μ(G_1 \sqcup G_2) / \sim_\mu.

In other words, the glueing is the disjoint union of the two graphs, with identification (and merging) of vertices that are related in μ\mu.

This allows us to define a rewrite on a graph GG:

Definition 3.9Graph rewrite

A rewrite rr on a graph G=(V,E)G = (V, E) is given by a tuple r=(GR,V,E,μ)r = (G_R, V^-, E^-, \mu), with

  • GR=(VR,ER)G_R = (V_R, E_R) is a graph called the replacement graph,
  • VVV^- \subseteq V is the vertex deletion set,
  • EEdom(μ)2E^- \subseteq E \cap dom(\mu)^2 is the edge deletion set, and
  • μ:VVR\mu: V^- \rightharpoonup V_R is the glueing relation, a partial function that maps a subset of the deleted vertices of GG to vertices in the replacement graph.

The domain of definition dom(μ)dom(\mu) is known as the boundary values of rr.

Define the context subgraph GC=(VC,EC)G_C = (V_C, E_C) of GG given by

VC=(VV)  dom(μ)EC=(EE)  VC2.\begin{aligned}V_C &= (V \smallsetminus V^-) \ \cup\ dom(\mu)\\E_C &= (E \smallsetminus E^-)\ \cap\ V_C^2.\end{aligned}

The partial function μ\mu is a special case of a glueing relation μVC×VR\mu \subseteq V_C \times V_R, and thus defines a glueing of GCG_C with GRG_R. The rewritten graph resulting from applying rr to GG is r(G)=(GCGR)/μ.r(G) = (G_C \sqcup G_R) / \sim_\mu.

An example of a graph rewrite is given in the next figure.

Application of a graph rewrite. On the left, the original graph GGG along with the replacement graph GRG_RGR​ (grey box). On the right, the rewritten graph r(G)r(G)r(G). Only the vertex ggg has been deleted, as other vertices in V−V^-V− are in the boundary dom(μ)dom(\mu)dom(μ) (in orange). The (singleton) edge deletion set is red. The blue edge connects a vertex of V∖V−V \smallsetminus V^-V∖V− 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 V∖V−V \smallsetminus V^-V∖V− to a non-boundary vertex of V−V^-V−, and is thus deleted.

Application of a graph rewrite. On the left, the original graph GG along with the replacement graph GRG_R (grey box). On the right, the rewritten graph r(G)r(G). Only the vertex gg has been deleted, as other vertices in VV^- are in the boundary dom(μ)dom(\mu) (in orange). The (singleton) edge deletion set is red. The blue edge connects a vertex of VVV \smallsetminus V^- 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 VVV \smallsetminus V^- to a non-boundary vertex of VV^-, and is thus deleted.

When there are no edges between VVV \smallsetminus V^- and Vdom(μ)V^- \smallsetminus dom(\mu) (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, 2017Filippo Bonchi, Fabio Gadducci, Aleks Kissinger, Paweł Sobociński and Fabio Zanasi. 2017. Confluence of Graph Rewriting with Interfaces or the supermaps of quantum causality Hefford, 2024James Hefford and Matt Wilson. 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 GG be a Σ\Sigma-typed minIR graph with data types TT and linear types TLTT_L \subseteq T. Consider type strings S,STS, S' \in T^\ast. We define the index sets

Idx(S)={iN1iS}IdxL(S)={iIdx(S)SiTL}Idx(S)\begin{aligned}\mathrm{Idx}(S) &= \{i \in \mathbb{N} \mid 1 \leq i \leq |S|\}\\\mathrm{Idx}_L(S) &= \{i \in \mathrm{Idx}(S) \mid S_i \in T_L\} \subseteq \mathrm{Idx}(S)\end{aligned}

corresponding respectively to the set of all indices into SS and the subset of indices of linear types. For any iIdx(S)i \in \mathrm{Idx}(S), we denote by SiS_i the type at position i in SS.

We define a partial order \preccurlyeq1 on TT^\ast where SSS \preccurlyeq S' and say that SS' can be coerced into SS if there exists an index map ρ:Idx(S)Idx(S)\rho: \mathrm{Idx}(S) \to \mathrm{Idx}(S') such that

  • types are preserved: Si=Sρ(i)S_i = S'_{\rho(i)}, and
  • ρ\rho is well-defined and bijective on the restriction to indices of linear types ρIdxL(S):IdxL(S)IdxL(S).\left.\rho\right|_{\mathrm{Idx}_L(S)}: \mathrm{Idx}_L(S) \to \mathrm{Idx}_L(S').
Definition 3.10Interface

Let TT be a set of data types. An interface I=(U,D)I = (U, D) is a pair of type strings U,DTU, D \in T^\ast.

We say that an interface I=(U,D)I' = (U', D') can be coerced into an interface I=(U,D)I = (U, D), written III \triangleleft I', if UUU \succcurlyeq U' and DDD \preccurlyeq D'.

We can define the interface associated with an operation oo in a minIR graph GG by considering the values used and defined by oo. Recall that typetype designates the type morphism on GG. We lift typetype to be defined on ordered list of values VV^\ast by element-wise application of the map and define the interface of oo in GG as

I(o)=(type(use(o)),type(def(o))).I(o) = (type(use(o)), type(\mathit{def}\,(o))).

Similarly, we can assign interfaces to subgraphs of minIR graphs:

Definition 3.11MinIR subgraph

Consider a subset of values and operations VHVV_H \subseteq V and OHOO_H \subseteq O. Define the use and define boundary sets

BU={vVHvdef(o) for some oOOH},BD={vVHvuse(o) for some oOOH}.\begin{aligned} B_U &= \{v \in V_H \mid v \in \mathit{def}\,(o)\textrm{ for some }o \in O \smallsetminus O_H \},\\B_D &= \{v \in V_H \mid v \in use(o)\textrm{ for some }o \in O \smallsetminus O_H \}.\end{aligned}

The tuple H=(VH,OH)H = (V_H, O_H) of GG is called a minIR subgraph of GG if there exists a region RR of GG such that all boundary values of HH are in RR:

B=BUBDR. B= B_U \cup B_D \subseteq R.

We write HGH \subseteq G to indicate that HH is a minIR subgraph of GG.

Notice that in (2), we lifted the \in notation to ordered list types VV^\ast: we write vSv \in S if there is iIdx(S)i \in \mathrm{Idx}(S) such that v=Siv = S_i.

Unlike interfaces, subgraph boundary values are not ordered. An ordering of BVB \subseteq V is a string SVS \in V^\ast along with a bijective map

ord:BIdx(S)such thatv=Sord(v).\mathrm{ord}: B \to \mathrm{Idx}(S) \quad\textrm{such that}\quad v = S_{\mathrm{ord}(v)}.

If there are strings SU,SDVS_U, S_D \in V^\ast and orderings of BUB_U and BDB_D

ordU: BUIdx(SU)ordD: BDIdx(SD),\begin{aligned}\textrm{ord}_U:\ &B_U \to \mathrm{Idx}(S_U)&\quad\textrm{ord}_D:\ &B_D \to \mathrm{Idx}(S_D),\end{aligned}

then we can set use(H)=SU\textit{use}\,(H) = S_U and def(H)=SD\textit{def}\,(H) = S_D in complete analogy to operations. We say that the subgraph HH implements the interface

IH=(type(use(H),type(def(H)).I_H = (type(\textit{use}\,(H), type(\textit{def}\,(H)).

Remark, though, that unlike operations, the same subgraph may implement more than one interface as a result of various choices of orderings ordU\textrm{ord}_U and ordD\textrm{ord}_D.

Importantly, a minIR subgraph HH is a graph but is in general not a valid minIR graph. A non-empty BUB_U use-boundary corresponds to values vBUv\in B_U without definitions in HH, and a non-empty BDTLB_D \cap T_L set corresponds to linear values in HH without uses. We can however always construct a valid minIR graph from HH by adding two operations oino_{in} and oouto_{out} in the root region, defined by

def(oin)=use(H),use(oout)=def(H),use(oin)=ε,def(oout)=ε.\begin{aligned}\textit{def}\,(o_{in}) &= use(H),\quad&\quad use(o_{out}) &= \textit{def}\,(H),\\use(o_{in}) &= \varepsilon,\quad&\quad\textit{def}\,(o_{out}) &= \varepsilon.\end{aligned}

where ε\varepsilon denotes the empty string in VV^\ast. We call the resulting graph Hˉ\bar{H} an interface graph. It implements the interface IHI_H if HH implements IHI_H. Calling to mind the illustrations of section 3.3, Hˉ\bar{H} looks like one of the nested regions within regiondef operations that we were considering.

MinIR operation rewrite #

Consider

  • an operation oo in a minIR graph GG with values V,V,
  • an interface graph Hˉ\bar{H} with values VHV_H and its associated subgraph HHˉH \subseteq \bar{H}, such that HH implements an interface I(o)IH=(type(use(H)),type(def(H)),I(o) \triangleleft I_H = (type(\textit{use}\,(H)), type(\textit{def}\,(H)),
  • the index maps ρ:Idx(use(H))Idx(use(o))\rho: \mathrm{Idx}(use(H)) \to \mathrm{Idx}(use(o)) and σ:Idx(def(o))Idx(def(H))\sigma: \mathrm{Idx}(\textit{def}\,(o)) \to \mathrm{Idx}(\textit{def}\,(H)) that define the generalisation I(o)IHI(o) \triangleleft I_H (per Definition 3.10).

We can define a glueing relation μoV×VH\mu_o \subseteq V \times V_H

μo= {(use(o)ρ(i),use(H)i)iIdx(use(H))} {(def(o)i,def(H)σ(i))iIdx(def(o))}.\begin{aligned}\mu_o =\ & \{ \left(use(o)_{\rho(i)}, use(H)_{i}\right) \mid i \in \mathrm{Idx}(use(H)) \}\ \cup \\& \{ \left(\mathit{def}\,(o)_{i}, \mathit{def}\,(H)_{\sigma(i)}\right) \mid i \in \mathrm{Idx}(\textit{def}\,(o)) \}.\end{aligned}

This is almost enough to define a rewrite that replaces the operation oo in GG with the values and operations of HH – the interface compatibility constraint I(o)IHI(o) \triangleleft I_H that we have imposed ensures that the resulting minIR graph is valid. Unfortunately, μo\mu_o is not a partial function as required by Definition 3.4.

This is resolved in the following proposition:

Proposition 3.5MinIR operation rewrite

Let GG, oo and HH such that I(o)IHI(o) \triangleleft I_H, as defined above. Then

((GH)/μo ⁣){o},\big((G \sqcup H) / \sim_{\mu_o}\!\big) \smallsetminus \{o\},

i.e. the graph obtained by removing the operation oo from the glueing of GG and HH along μo\mu_o, is a valid minIR graph.

There is a graph GRG_R with values VRV_R and a partial function μo:VVR\mu_o': V \rightharpoonup V_R such that the graph (5) is the graph ro(G)r_o(G), obtained from the rewrite

ro=(GR,dom(μo),{o},μo).r_o = (G_R, dom(\mu_o), \{o\}, \mu_o').

We call ror_o the rewrite of oo into HH.

The definition of the rewrite of oo into a graph HH 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 ooo in the graph GGG (top left) into the operations o1o_1o1​ and o2o_2o2​ of the graph Hˉ\bar{H}Hˉ (bottom left). Coloured dots indicate the index maps ρ\rhoρ and σ\sigmaσ from inputs of Hˉ\bar{H}Hˉ to inputs of ooo, respectively from outputs of ooo to outputs of Hˉ\bar{H}Hˉ.

Rewriting operation oo in the graph GG (top left) into the operations o1o_1 and o2o_2 of the graph Hˉ\bar{H} (bottom left). Coloured dots indicate the index maps ρ\rho and σ\sigma from inputs of Hˉ\bar{H} to inputs of oo, respectively from outputs of oo to outputs of Hˉ\bar{H}.

When the index maps ρ\rho and σ\sigma 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 oo (yellow and red dots). This will never happen with linear values (as they can never have more than one use in oo), nor with any value definitions (the same value can never be defined more than once). Finally, values not in the image of ρ\rho or σ\sigma (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 GRG_R and μo\mu_o'. Define R(VH)2\sim_R \subseteq (V_H)^2 as the smallest equivalence relation such that

use(o)ρ(i)=use(o)ρ(j)def(oin)iRdef(oin)j.use(o)_{\rho(i)} = use(o)_{\rho(j)} \Rightarrow \textit{def}\,(o_{in})_i \sim_R \textit{def}\,(o_{in})_j.

Then we define GˉR=Hˉ/R\bar{G}_R = \bar{H} / \sim_R, the graph obtained by glueing together values within the same equivalence class of R\sim_R.

Claim 1: GˉR\bar{G}_R is a valid minIR graph.

Claim 1 follows from the observation that only values of non-linear types are glued together. If vRvv \sim_R v', then either v=vv = v' or there exist iji \neq j such that def(oin)iRdef(oin)j.\textit{def}\,(o_{in})_i \sim_R \textit{def}\,(o_{in})_j. If ρ(i)=ρ(j)\rho(i) = \rho(j), then ρ\rho is not injective on ii and jj, and by the definition of ρ\rho, type(v)TLtype(v)\notin T_L and type(v)TLtype(v') \notin T_L. Otherwise, there are i=ρ(i)use(o)ρ(j)=ji' = \rho(i) \neq use(o)_{\rho(j)} = j' such that use(o)i=use(o)juse(o)_{i'} = use(o)_{j'}. The same value is used twice, which is only a valid minIR graph if vv and vv' are not linear, thus proving Claim 1.

Define GRG_R as the subgraph obtained from GˉR\bar{G}_R by removing the operations {oin,oout}\{o_{in}, o_{out}\}. Let VR=VH/RV_R = V_H / \sim_R be the set of values of GRG_R (and of GˉR\bar{G}_R). Writing αR(v)VR\alpha_R(v) \in V_R for the equivalence class of R\sim_R that vVHv \in V_H belongs to, we can define μoV×VR\mu_o' \in V \times V_R as:

(v,w)μo(v,αR(w))μo.(v, w) \in \mu_o \Leftrightarrow (v, \alpha_R(w)) \in \mu_o'.

Claim 2: μo\mu_o' is a partial function VVRV \rightharpoonup V_R.

In other words, for all (v,α1),(v,α2)μo(v, \alpha_1), (v, \alpha_2) \in \mu_o', then α1=α2\alpha_1 = \alpha_2. Let w1α1w_1 \in \alpha_1 and w2α2w_2 \in \alpha_2 be values in VHV_H. First of all, use(o)idef(o)juse(o)_i \neq \textit{def}\,(o)_j for all i,ji, j, otherwise GG is not acyclic. So either vuse(o)v \in use(o), or vdef(o)v \in \textit{def}\,(o), but not both.

The simpler case: if vdef(o)v \in \textit{def}\,(o), then there exists ii such that def(o)i=v\textit{def}\,(o)_i = v. Furthermore ii is unique because by minIR definition, vv has a unique definition in GG. It follows from (4) that w1=use(oout)ρ(i)=w2w_1 = use(o_{out})_{\rho(i)} = w_2 and hence α1=α2\alpha_1 = \alpha_2.

Otherwise, there exists ii and jj such that v=use(o)ρ(i)=use(o)ρ(j)v = use(o)_{\rho(i)} = use(o)_{\rho(j)} and def(oin)i=w1\textit{def}\,(o_{in})_i = w_1 as well as def(oin)j=w2\textit{def}\,(o_{in})_j = w_2. By definition of R\sim_R, we have wRww \sim_R w', and thus

α1=αR(w1)=αR(w2)=α2,\alpha_1 = \alpha_R(w_1) = \alpha_R(w_2) = \alpha_2,

proving Claim 2.

Claim 3: ro(G)r_o(G) is given by ((GH)/μo){o}((G \sqcup H) / \sim_{\mu_o}) \smallsetminus \{o\}.

It follows directly from our construction of R\sim_R and μo\mu_o' that the equivalence classes of (the smallest equivalence relation closure of) μoαR\mu_o' \circ \alpha_R is equal to the equivalence classes of (the smallest equivalence relation closure of) μo\mu_o. The claim follows by Definition 3.8 and the definition of ror_o.

And finally, Claim 4: ro(G)r_o(G) 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 GG and HH are acyclic and a single operation oo in GG is replaced: any cycle across GG and HH would also be a cycle in GG by replacing the subpath in HH with oo. (iv) follows from the fact that oino_{in} and oouto_{out} are in the root region of Hˉ\bar{H}, by definition of interface implementation. (i): removing oo from GG removes the unique definitions of all values in def(o)\mathit{def}\,(o). Each such value vv is glued to a unique value use(oout)i\mathit{use}\,(o_{out})_i in HH – the new and unique definition of vv in ro(G).r_o(G). (ii) follows from the same argument as in (i), but relying on injectivity of ρ\rho on linear values to establish uniqueness.

Arbitrary minIR rewrites #

We have so far defined rewrites of single operations into graphs HH. We can generalise these rewrites to rewrite subgraphs PGP \subseteq G, provided the minIR subgraphs satisfy some constraints. We require for this a notion of convexity, as discussed in Bonchi, 2022Filippo Bonchi, Fabio Gadducci, Aleks Kissinger, Pawel Sobocinski and Fabio Zanasi. 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 G=(V,VL,O,def,use,parent),G = (V, V_L, O, \mathit{def}, \mathit{use}, \mathit{parent}), along with a subgraph of GG that we will now call P=(VP,OP)GP = (V_P, O_P) \subseteq G, to distinguish from HH.

Definition 3.12Convex minIR subgraph

A minIR subgraph PGP \subseteq G is convex if the following conditions hold:

  • for all v1,v2VPv_1, v_2 \in V_P, any path along \leadsto from v1v_1 to v2v_2 contains only vertices in VPV_P,
  • parent-child relations are contained within the subgraph, i.e. vVPdom(parent)parent(v)VP.v \in V_P \cap dom(\mathit{parent}) \Leftrightarrow \mathit{parent}(v) \in V_P.

Define the sets of boundary values BU,BDB_U, B_D and B=BUBDB = B_U \cup B_D, as in (2); then fix the boundary orderings use(P)use(P) and def(P)\textit{def}\,(P) as in (3). The subgraph PP implements the interface

IP=(type(use(P)),type(def(P))).I_P = (type(use(P)), type(\textit{def}\,(P))).

Consider an interface graph Hˉ\bar{H} that implements IHI_H such that IPIHI_P \triangleleft I_H. Instead of defining a gluing relation from values of an operation oo to values of HH, we replace the interface I(o)I(o) with IPI_P. This generalises the definition of μo\mu_o from (4) to a glueing μB×VH\mu\subseteq B \times V_H defined as

μ= {((use(P))ρ(i),use(H)i)iIdx(use(H))} {((def(P))i,def(H)σ(i))iIdx(def(P))},\begin{aligned}\mu =\ & \{ \left((use(P))_{\rho(i)}, use\,(H)_{i}\right) \mid i \in \mathrm{Idx}(use(H)) \}\ \cup \\& \{ \left((\textit{def}\,(P))_{i}, \textit{def}\,(H)_{\sigma(i)}\right) \mid i \in \mathrm{Idx}(\textit{def}\,(P)) \},\end{aligned}

With the set of boundary operations defined as2

OB={oOP(def(o)use(o))B},O_B = \left\{o \in O_P \mid \left(\mathit{def}\,(o) \cup use(o)\right) \subseteq B\right\},

we are able to define minIR rewrites in their most general form.

Proposition 3.6MinIR subgraph rewrite

Let PGP \subseteq G and HH such that IPIHI_P \triangleleft I_H and PP is convex, as defined above. Then,

((GH)/μ ⁣)(VPB,OB),\big((G \sqcup H) / \sim_{\mu}\!\big) \smallsetminus (V_P \smallsetminus B, O_B),

i.e. the graph obtained by removing the values VPBV_P \smallsetminus B and operations OBO_B from the glueing of GG and HH along μ\mu, is a valid minIR graph.

There is a graph GRG_R with values VRV_R and a partial function μ:VPVR\mu': V_P \rightharpoonup V_R such that the graph (8) is the graph rP(G)r_P(G), obtained from the rewrite

rP=(GR,VP,OB,μ).r_P = (G_R, V_P, O_B, \mu').

We call rPr_P the rewrite of PP into HH.

Proof

Consider an operation oo that implements IP=(UP,DP)I_P = (U_P, D_P). We can define the interface graph Hˉo\bar{H}_o given by three operations oino_{in}, oouto_{out} and oo. Its associated subgraph HoHˉoH_o \subseteq \bar{H}_o only includes oo. Let μ~\tilde \mu be the glueing relation

μ~= {(use(P)i,use(o)i)iIdx(UP)} {(def(P)i,def(o)i)iIdx(DP)}.\begin{aligned}\tilde \mu =\ &\{ (use(P)_i, use(o)_i) \mid i \in \mathrm{Idx}(U_P) \}\ \cup \\& \{ (\textit{def}\,(P)_i, \textit{def}\,(o)_i) \mid i \in \mathrm{Idx}(D_P) \}.\end{aligned}

Consider the rewrite r=(Ho,VPB,OB,μ~)r = (H_o, V_P \smallsetminus B, O_B, \tilde\mu). If we write G=(V,O)G' = (V', O') for the subgraph of GG given by V=(V(VPB))O=(OOB)(V),\begin{aligned}V' &= (V \smallsetminus (V_P \smallsetminus B)) \\O' &= (O \smallsetminus O_B) \cap (V')^\ast,\end{aligned} then according to (1), the graph resulting from applying rr to GG can be expressed as the glueing

Go=r(G)=(GHo)/μ~.G_o = r(G) = (G' \sqcup H_o) / \sim_{\tilde \mu}.

Our claim is that GoG_o is a valid minIR graph.

The graph (8) is then obtained by applying the rewrite ror_o as given by (6) to GoG_o. Defining the rewrite rPr_P as the composition of rr followed by ror_o, 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 GG' is obtained by removing values and operations from a valid minIR graph GG, no value in VV' can be defined more than once. A value vVv \in V' that is not defined in GG' must be in the boundary vBv \in B of PP. By the boundary definitions of (2), vv cannot be in BUB_U and thus must be in BDB_D. It follows by the definition of the glueing μ~\tilde \mu that in GoG_o, vv will be in the definitions of oo: vdef(o)v \in \textit{def}\,(o). The glueing μ~\tilde \mu is bijective between the values of PP and oo and thus we can conclude that vv has a unique definition in GoG_o.

The same argument applies to property ii). Property iii) follows from the convexity requirement of PP. Finally, property iv) (every region has at most one parent) follows from two observations. First, by convexity of PP, no deleted value or operation could be the parent of any value not in PP, and thus the parentparent relation is well-defined on GG': im(parent)Oim(parent) \subseteq O'. Secondly, all new values and operations added to the boundary region of GG' are from the root region of HH, 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 vv belongs to a region that is not the top level region of the subgraph3, we can repeatedley hoist vv 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.

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.


  1. To be precise, \preccurlyeq is a partial order on the type strings up to isomorphism↩︎

  2. The set operations \subseteq and \cup are again understood to apply to the unordered set of elements contained in the lists def(o)\mathit\,{def}(o) and use(o)\mathit{use}\,(o)↩︎

  3. We can always extend a subgraph to contain more ancestor regions, until there is indeed a unique top-level region in the subgraph. ↩︎