### 04b Rewrite Operational

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 G 1=(V 1, E 2)G_1 = (V_1, E_2)G 1​=(V 1​,E 2​) and G 2=(V 2, E 2)G_2 = (V_2, E_2)G 2​=(V 2​,E 2​), along with a relation μ ⊆V 1×V 2\mu\ \subseteq V_1 \times V_2μ ⊆V 1​×V 2​. Let ∼μ ⊆(V 1⊔V 2)2\sim_\mu \ \subseteq (V_1 \sqcup V_2)^2∼μ​ ⊆(V 1​⊔V 2​)2 be the equivalence relation induced by μ\muμ, i.e. the smallest relation on V 1⊔V 2 V_1 \sqcup V_2 V 1​⊔V 2​ that is reflexive, symmetric and transitive, and satisifes for all v 1∈V 1 v_1 \in V_1 v 1​∈V 1​ and v 2∈V 2 v_2 \in V_2 v 2​∈V 2​,

(v 1, v 2)∈μ⇒v 1∼μ v 2.(v_1, v_2) \in \mu \Rightarrow v_1 \sim_\mu v_2.(v 1​,v 2​)∈μ⇒v 1​∼μ​v 2​.

Then, we can define

- V=(V 1⊔V 2)/∼μ V = (V_1 \sqcup V_2)/\sim_\mu V=(V 1​⊔V 2​)/∼μ​ is the set of all equivalence classes of ∼μ\sim_\mu∼μ​, and
- for v∈V 1⊔V 2 v \in V_1 \sqcup V_2 v∈V 1​⊔V 2​, α μ(v)∈V\alpha_\mu(v) \in V α μ​(v)∈V is the equivalence class of ∼μ\sim_\mu∼μ​ that v v v belongs to.

Definition 3.7Graph glueing

The glueing of G 1 G_1G 1​ and G 2 G_2G 2​ according to the glueing relation μ\muμ is given by the vertices V=(V 1⊔V 2)/∼μ V = (V_1 \sqcup V_2)/\sim_\mu V=(V 1​⊔V 2​)/∼μ​ and the edges

E={(α μ(u),α μ(v))∣(u, v)∈E 1⊔E 2}⊆V 2. E = \{(\alpha_\mu(u), \alpha_\mu(v)) \mid (u,v) \in E_1 \sqcup E_2 \} \subseteq V^2. E={(α μ​(u),α μ​(v))∣(u, v)∈E 1​⊔E 2​}⊆V 2.

We write the glueing graph as (G 1⊔G 2)/∼μ(G_1 \sqcup G_2) / \sim_\mu(G 1​⊔G 2​)/∼μ​.

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 G GG:

Definition 3.8Graph rewrite

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

- G R=(V R, E R)G_R = (V_R, E_R)G R​=(V R​,E R​) is a graph called the replacement graph,
- V−⊆V V^- \subseteq V V−⊆V is the vertex deletion set,
- E−⊆E∩d o m(μ)2 E^- \subseteq E \cap dom(\mu)^2 E−⊆E∩dom(μ)2 is the edge deletion set, and
- μ: V−⇀V R\mu: V^- \rightharpoonup V_Rμ: V−⇀V R​ is the glueing relation, a partial function that maps a subset of the deleted vertices of G GG to vertices in the replacement graph.

The domain of definition d o m(μ)dom(\mu)dom(μ) is known as the boundary values of r r r.

Define the context subgraph G C=(V C, E C)G_C = (V_C, E_C)G C​=(V C​,E C​) of G GG given by

V C=(V∖V−) ∪ d o m(μ)E C=(E∖E−) ∩ V C 2.\begin{aligned}V_C &= (V \smallsetminus V^-) \ \cup\ dom(\mu)\\E_C &= (E \smallsetminus E^-)\ \cap\ V_C^2.\end{aligned}V C​E C​​=(V∖V−) ∪ dom(μ)=(E∖E−) ∩ V C 2​.​

The partial function μ\muμ is a special case of a glueing relation μ⊆V C×V R\mu \subseteq V_C \times V_Rμ⊆V C​×V R​, and thus defines a glueing of G C G_CG C​ with G R G_RG R​. The rewritten graph resulting from applying r r r to G GG is r(G)=(G C⊔G R)/∼μ. r(G) = (G_C \sqcup G_R) / \sim_\mu. r(G)=(G C​⊔G R​)/∼μ​.

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

Application of a graph rewrite. On the left, the original graph G GG along with the replacement graph G R G_RG R​ (grey box). On the right, the rewritten graph r(G)r(G)r(G). Only the vertex g g g has been deleted, as other vertices in V−V^-V− are in the boundary d o m(μ)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.

When there are no edges between V∖V−V \smallsetminus V^-V∖V− and V−∖d o m(μ)V^- \smallsetminus dom(\mu)V−∖dom(μ) (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 G GG be a Σ\SigmaΣ-typed minIR graph with data types T T T and linear types T L⊆T T_L \subseteq T T L​⊆T. Consider type strings S, S′∈T∗S, S' \in T^\ast S, S′∈T∗. We define the index sets

I d x(S)={i∈N∣1≤i≤∣S∣}I d x L(S)={i∈I d x(S)∣S i∈T L}⊆I d x(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}Idx(S)Idx L​(S)​={i∈N∣1≤i≤∣S∣}={i∈Idx(S)∣S i​∈T L​}⊆Idx(S)​

corresponding respectively to the set of all indices into S S S and the subset of indices of linear types. For any i∈I d x(S)i \in \mathrm{Idx}(S)i∈Idx(S), we denote by S i S_i S i​ the type at position i in S S S.

We define a partial order ≼\preccurlyeq≼1 on T∗T^\ast T∗ where S≼S′S \preccurlyeq S'S≼S′ and say that S′S'S′ can be coerced into S S S if there exists an index map ρ: I d x(S)→I d x(S′)\rho: \mathrm{Idx}(S) \to \mathrm{Idx}(S')ρ: Idx(S)→Idx(S′) such that

- types are preserved: S i=S ρ(i)′S_i = S'_{\rho(i)}S i​=S ρ(i)′​, and
- ρ\rhoρ is well-defined and bijective on the restriction to indices of linear types ρ∣I d x L(S):I d x L(S)→I d x L(S′).\left.\rho\right|_{\mathrm{Idx}_L(S)}: \mathrm{Idx}_L(S) \to \mathrm{Idx}_L(S').ρ∣Idx L​(S)​:Idx L​(S)→Idx L​(S′).

Definition 3.9Interface

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

We say that an interface I′=(U′,D′)I' = (U', D')I′=(U′,D′) can be coerced into an interface I=(U, D)I = (U, D)I=(U, D), written I◃I′I \triangleleft I'I◃I′, if U≽U′U \succcurlyeq U'U≽U′ and D≼D′D \preccurlyeq D'D≼D′.

We can define the interface associated with an operation o oo in a minIR graph G GG by considering the values used and defined by o oo. Recall that t y p e typet y pe designates the type morphism on G GG. We lift t y p e typet y pe to be defined on ordered list of values V∗V^\ast V∗ by element-wise application of the map and define the interface of o oo in G GG as

I(o)=(t y p e(u s e(o)),t y p e(d e f (o))).I(o) = (type(use(o)), type(\mathit{def}\,(o))).I(o)=(t y pe(use(o)),t y pe(def(o))).

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

Definition 3.10MinIR subgraph

Consider a subset of values and operations V H⊆V V_H \subseteq V V H​⊆V and O H⊆O O_H \subseteq O O H​⊆O. Define the use and define boundary sets

B U={v∈V H∣v∈d e f (o) for some o∈O∖O H},B D={v∈V H∣v∈u s e(o) for some o∈O∖O H}.\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}B U​B D​​={v∈V H​∣v∈def(o) for some o∈O∖O H​},={v∈V H​∣v∈use(o) for some o∈O∖O H​}.​

The tuple H=(V H, O H)H = (V_H, O_H)H=(V H​,O H​) of G GG is called a minIR subgraph of G GG if there exists a region R R R of G GG such that all boundary values of H H H are in R R R:

B=B U∪B D⊆R. B= B_U \cup B_D \subseteq R. B=B U​∪B D​⊆R.

We write H⊆G H \subseteq G H⊆G to indicate that H H H is a minIR subgraph of G GG.

Notice that in (2), we lifted the ∈\in∈ notation to ordered list types V∗V^\ast V∗: we write v∈S v \in S v∈S if there is i∈I d x(S)i \in \mathrm{Idx}(S)i∈Idx(S) such that v=S i v = S_i v=S i​.

Unlike interfaces, subgraph boundary values are not ordered. An ordering of B⊆V B \subseteq V B⊆V is a string S∈V∗S \in V^\ast S∈V∗ along with a bijective map

o r d: B→I d x(S)such that v=S o r d(v).\mathrm{ord}: B \to \mathrm{Idx}(S) \quad\textrm{such that}\quad v = S_{\mathrm{ord}(v)}.ord: B→Idx(S)such that v=S ord(v)​.

If there are strings S U, S D∈V∗S_U, S_D \in V^\ast S U​,S D​∈V∗ and orderings of B U B_U B U​ and B D B_D B D​

ord U: B U→I d x(S U)ord D: B D→I d x(S D),\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}ord U​: ​B U​→Idx(S U​)​ord D​: ​B D​→Idx(S D​),​

then we can set use (H)=S U\textit{use}\,(H) = S_Uuse(H)=S U​ and def (H)=S D\textit{def}\,(H) = S_Ddef(H)=S D​ in complete analogy to operations. We say that the subgraph H H H implements the interface

I H=(t y p e(use (H),t y p e(def (H)).I_H = (type(\textit{use}\,(H), type(\textit{def}\,(H)).I H​=(t y pe(use(H),t y pe(def(H)).

Remark, though, that unlike operations, the same subgraph may implement more than one interface as a result of various choices of orderings ord U\textrm{ord}_Uord U​ and ord D\textrm{ord}_Dord D​.

Importantly, a minIR subgraph H H H is a graph but is in general not a valid minIR graph. A non-empty B U B_U B U​ use-boundary corresponds to values v∈B U v\in B_U v∈B U​ without definitions in H H H, and a non-empty B D∩T L B_D \cap T_L B D​∩T L​ set corresponds to linear values in H H H without uses. We can however always construct a valid minIR graph from H H H by adding two operations o i n o_{in}o in​ and o o u t o_{out}o out​ in the root region, defined by

def (o i n)=u s e(H),u s e(o o u t)=def (H),u s e(o i n)=ε, def (o o u t)=ε.\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}def(o in​)use(o in​)​=use(H),=ε,​use(o out​)def(o out​)​=def(H),=ε.​

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

#### MinIR operation rewrite #

Consider

- an operation o oo in a minIR graph G GG with values V, V, V,
- an interface graph H ˉ\bar{H}H ˉ with values V H V_H V H​ and its associated subgraph H⊆H ˉ H \subseteq \bar{H}H⊆H ˉ, such that H H H implements an interface I(o)◃I H=(t y p e(use (H)),t y p e(def (H)),I(o) \triangleleft I_H = (type(\textit{use}\,(H)), type(\textit{def}\,(H)),I(o)◃I H​=(t y pe(use(H)),t y pe(def(H)),
- the index maps ρ: I d x(u s e(H))→I d x(u s e(o))\rho: \mathrm{Idx}(use(H)) \to \mathrm{Idx}(use(o))ρ: Idx(use(H))→Idx(use(o)) and σ: I d x(def (o))→I d x(def (H))\sigma: \mathrm{Idx}(\textit{def}\,(o)) \to \mathrm{Idx}(\textit{def}\,(H))σ: Idx(def(o))→Idx(def(H)) that define the generalisation I(o)◃I H I(o) \triangleleft I_H I(o)◃I H​ (per Definition 3.9).

We can define a glueing relation μ o⊆V×V H\mu_o \subseteq V \times V_Hμ o​⊆V×V H​

μ o= {(u s e(o)ρ(i),u s e(H)i)∣i∈I d x(u s e(H))} ∪{(d e f (o)i, d e f (H)σ(i))∣i∈I d x(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}μ o​= ​{(use(o)ρ(i)​,use(H)i​)∣i∈Idx(use(H))} ∪{(def(o)i​,def(H)σ(i)​)∣i∈Idx(def(o))}.​

This is almost enough to define a rewrite that replaces the operation o oo in G GG with the values and operations of H H H – the interface compatibility constraint I(o)◃I H I(o) \triangleleft I_H I(o)◃I H​ that we have imposed ensures that the resulting minIR graph is valid. Unfortunately, μ o\mu_oμ 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 G GG, o oo and H H H such that I(o)◃I H I(o) \triangleleft I_H I(o)◃I H​, as defined above. Then

((G⊔H)/∼μ o⁣)∖{o},\big((G \sqcup H) / \sim_{\mu_o}\!\big) \smallsetminus \{o\},((G⊔H)/∼μ o​​)∖{o},

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

There is a graph G R G_RG R​ with values V R V_R V R​ and a partial function μ o′:V⇀V R\mu_o': V \rightharpoonup V_Rμ o′​:V⇀V R​ such that the graph (5) is the graph r o(G)r_o(G)r o​(G), obtained from the rewrite

r o=(G R, d o m(μ o),{o},μ o′).r_o = (G_R, dom(\mu_o), \{o\}, \mu_o').r o​=(G R​,dom(μ o​),{o},μ o′​).

We call r o r_o r o​ the rewrite of o oo into H H H.

The definition of the rewrite of o oo into a graph H H H 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 o oo in the graph G GG (top left) into the operations o 1 o_1o 1​ and o 2 o_2o 2​ 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 o oo, respectively from outputs of o oo to outputs of H ˉ\bar{H}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 o oo (yellow and red dots). This will never happen with linear values (as they can never have more than one use in o 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 G R G_RG R​ and μ o′\mu_o'μ o′​. Define ∼R⊆(V H)2\sim_R \subseteq (V_H)^2∼R​⊆(V H​)2 as the smallest equivalence relation such that

u s e(o)ρ(i)=u s e(o)ρ(j)⇒def (o i n)i∼R def (o i n)j. use(o)_{\rho(i)} = use(o)_{\rho(j)} \Rightarrow \textit{def}\,(o_{in})_i \sim_R \textit{def}\,(o_{in})_j.use(o)ρ(i)​=use(o)ρ(j)​⇒def(o in​)i​∼R​def(o in​)j​.

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

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

Claim 1 follows from the observation that only values of non-linear types are glued together. If v∼R v′v \sim_R v'v∼R​v′, then either v=v′v = v'v=v′ or there exist i≠j i \neq ji=j such that def (o i n)i∼R def (o i n)j.\textit{def}\,(o_{in})_i \sim_R \textit{def}\,(o_{in})_j.def(o in​)i​∼R​def(o in​)j​. If ρ(i)=ρ(j)\rho(i) = \rho(j)ρ(i)=ρ(j), then ρ\rhoρ is not injective on i ii and j j j, and by the definition of ρ\rhoρ, t y p e(v)∉T L type(v)\notin T_Lt y pe(v)∈/T L​ and t y p e(v′)∉T L type(v') \notin T_Lt y pe(v′)∈/T L​. Otherwise, there are i′=ρ(i)≠u s e(o)ρ(j)=j′i' = \rho(i) \neq use(o)_{\rho(j)} = j'i′=ρ(i)=use(o)ρ(j)​=j′ such that u s e(o)i′=u s e(o)j′use(o)_{i'} = use(o)_{j'}use(o)i′​=use(o)j′​. The same value is used twice, which is only a valid minIR graph if v v v and v′v'v′ are not linear, thus proving Claim 1.

Define G R G_RG R​ as the subgraph obtained from G ˉ R\bar{G}_R G ˉ R​ by removing the operations {o i n, o o u t}\{o_{in}, o_{out}\}{o in​,o out​}. Let V R=V H/∼R V_R = V_H / \sim_R V R​=V H​/∼R​ be the set of values of G R G_RG R​ (and of G ˉ R\bar{G}_R G ˉ R​). Writing α R(v)∈V R\alpha_R(v) \in V_R α R​(v)∈V R​ for the equivalence class of ∼R\sim_R∼R​ that v∈V H v \in V_H v∈V H​ belongs to, we can define μ o′∈V×V R\mu_o' \in V \times V_Rμ o′​∈V×V R​ as:

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

Claim 2: μ o′\mu_o'μ o′​ is a partial function V⇀V R V \rightharpoonup V_R V⇀V R​.

In other words, for all (v, α 1),(v, α 2)∈μ o′(v, \alpha_1), (v, \alpha_2) \in \mu_o'(v, α 1​),(v, α 2​)∈μ o′​, then α 1=α 2\alpha_1 = \alpha_2 α 1​=α 2​. Let w 1∈α 1 w_1 \in \alpha_1 w 1​∈α 1​ and w 2∈α 2 w_2 \in \alpha_2 w 2​∈α 2​ be values in V H V_H V H​. First of all, u s e(o)i≠def (o)j use(o)_i \neq \textit{def}\,(o)_juse(o)i​=def(o)j​ for all i, j i, ji, j, otherwise G GG is not acyclic. So either v∈u s e(o)v \in use(o)v∈use(o), or v∈def (o)v \in \textit{def}\,(o)v∈def(o), but not both.

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

Otherwise, there exists i ii and j j j such that v=u s e(o)ρ(i)=u s e(o)ρ(j)v = use(o)_{\rho(i)} = use(o)_{\rho(j)}v=use(o)ρ(i)​=use(o)ρ(j)​ and def (o i n)i=w 1\textit{def}\,(o_{in})_i = w_1def(o in​)i​=w 1​ as well as def (o i n)j=w 2\textit{def}\,(o_{in})_j = w_2def(o in​)j​=w 2​. By definition of ∼R\sim_R∼R​, we have w∼R w′w \sim_R w'w∼R​w′, and thus

α 1=α R(w 1)=α R(w 2)=α 2,\alpha_1 = \alpha_R(w_1) = \alpha_R(w_2) = \alpha_2, α 1​=α R​(w 1​)=α R​(w 2​)=α 2​,

proving Claim 2.

Claim 3: r o(G)r_o(G)r o​(G) is given by ((G⊔H)/∼μ o)∖{o}((G \sqcup H) / \sim_{\mu_o}) \smallsetminus \{o\}((G⊔H)/∼μ o​​)∖{o}.

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

And finally, Claim 4: r o(G)r_o(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 G GG and H H H are acyclic and a single operation o oo in G GG is replaced: any cycle across G GG and H H H would also be a cycle in G GG by replacing the subpath in H H H with o oo. (iv) follows from the fact that o i n o_{in}o in​ and o o u t o_{out}o out​ are in the root region of H ˉ\bar{H}H ˉ, by definition of interface implementation. (i): removing o oo from G GG removes the unique definitions of all values in d e f (o)\mathit{def}\,(o)def(o). Each such value v v v is glued to a unique value u s e (o o u t)i\mathit{use}\,(o_{out})_iuse(o out​)i​ in H H H – the new and unique definition of v v v in r o(G).r_o(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 H H H. We can generalise these rewrites to rewrite subgraphs P⊆G P \subseteq G P⊆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, V L, O, d e f, u s e, p a r e n t),G = (V, V_L, O, \mathit{def}, \mathit{use}, \mathit{parent}),G=(V, V L​,O, def, use, parent), along with a subgraph of G GG that we will now call P=(V P, O P)⊆G P = (V_P, O_P) \subseteq G P=(V P​,O P​)⊆G, to distinguish from H H H.

Definition 3.11Convex minIR subgraph

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

- for all v 1, v 2∈V P v_1, v_2 \in V_P v 1​,v 2​∈V P​, any path along ⇝\leadsto⇝ from v 1 v_1 v 1​ to v 2 v_2 v 2​ contains only vertices in V P V_P V P​,
- parent-child relations are contained within the subgraph, i.e. v∈V P∩d o m(p a r e n t)⇔p a r e n t(v)∈V P. v \in V_P \cap dom(\mathit{parent}) \Leftrightarrow \mathit{parent}(v) \in V_P. v∈V P​∩dom(parent)⇔parent(v)∈V P​.

Define the sets of boundary values B U, B D B_U, B_D B U​,B D​ and B=B U∪B D B = B_U \cup B_D B=B U​∪B D​, as in (2); then fix the boundary orderings u s e(P)use(P)use(P) and def (P)\textit{def}\,(P)def(P) as in (3). The subgraph P P P implements the interface

I P=(t y p e(u s e(P)),t y p e(def (P))).I_P = (type(use(P)), type(\textit{def}\,(P))).I P​=(t y pe(use(P)),t y pe(def(P))).

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

μ= {((u s e(P))ρ(i),u s e (H)i)∣i∈I d x(u s e(H))} ∪{((def (P))i, def (H)σ(i))∣i∈I d x(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}μ= ​{((use(P))ρ(i)​,use(H)i​)∣i∈Idx(use(H))} ∪{((def(P))i​,def(H)σ(i)​)∣i∈Idx(def(P))},​

With the set of boundary operations defined as2

O B={o∈O P∣(d e f (o)∪u s e(o))⊆B},O_B = \left\{o \in O_P \mid \left(\mathit{def}\,(o) \cup use(o)\right) \subseteq B\right\},O B​={o∈O P​∣(def(o)∪use(o))⊆B},

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

Proposition 3.6MinIR subgraph rewrite

Let P⊆G P \subseteq G P⊆G and H H H such that I P◃I H I_P \triangleleft I_H I P​◃I H​ and P P P is convex, as defined above. Then,

((G⊔H)/∼μ⁣)∖(V P∖B, O B),\big((G \sqcup H) / \sim_{\mu}\!\big) \smallsetminus (V_P \smallsetminus B, O_B),((G⊔H)/∼μ​)∖(V P​∖B, O B​),

i.e. the graph obtained by removing the values V P∖B V_P \smallsetminus B V P​∖B and operations O B O_B O B​ from the glueing of G GG and H H H along μ\muμ, is a valid minIR graph.

There is a graph G R G_RG R​ with values V R V_R V R​ and a partial function μ′:V P⇀V R\mu': V_P \rightharpoonup V_Rμ′:V P​⇀V R​ such that the graph (8) is the graph r P(G)r_P(G)r P​(G), obtained from the rewrite

r P=(G R, V P, O B, μ′).r_P = (G_R, V_P, O_B, \mu').r P​=(G R​,V P​,O B​,μ′).

We call r P r_P r P​ the rewrite of P P P into H H H.

Proof

Consider an operation o oo that implements I P=(U P, D P)I_P = (U_P, D_P)I P​=(U P​,D P​). We can define the interface graph H ˉ o\bar{H}_o H ˉ o​ given by three operations o i n o_{in}o in​, o o u t o_{out}o out​ and o oo. Its associated subgraph H o⊆H ˉ o H_o \subseteq \bar{H}_o H o​⊆H ˉ o​ only includes o oo. Let μ~\tilde \mu μ~​ be the glueing relation

μ~= {(u s e(P)i, u s e(o)i)∣i∈I d x(U P)} ∪{(def (P)i, def (o)i)∣i∈I d x(D P)}.\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}μ~​= ​{(use(P)i​,use(o)i​)∣i∈Idx(U P​)} ∪{(def(P)i​,def(o)i​)∣i∈Idx(D P​)}.​

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

G o=r(G)=(G′⊔H o)/∼μ~.G_o = r(G) = (G' \sqcup H_o) / \sim_{\tilde \mu}.G o​=r(G)=(G′⊔H o​)/∼μ~​​.

Our claim is that G o G_oG o​ is a valid minIR graph.

The graph (8) is then obtained by applying the rewrite r o r_o r o​ as given by (6) to G o G_oG o​. Defining the rewrite r P r_P r P​ as the composition of r r r followed by r o r_o r 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 G′G'G′ is obtained by removing values and operations from a valid minIR graph G GG, no value in V′V'V′ can be defined more than once. A value v∈V′v \in V'v∈V′ that is not defined in G′G'G′ must be in the boundary v∈B v \in B v∈B of P P P. By the boundary definitions of (2), v v v cannot be in B U B_U B U​ and thus must be in B D B_D B D​. It follows by the definition of the glueing μ~\tilde \mu μ~​ that in G o G_oG o​, v v v will be in the definitions of o oo: v∈def (o)v \in \textit{def}\,(o)v∈def(o). The glueing μ~\tilde \mu μ~​ is bijective between the values of P P P and o oo and thus we can conclude that v v v has a unique definition in G o G_oG o​.

The same argument applies to property ii). Property iii) follows from the convexity requirement of P P P. Finally, property iv) (every region has at most one parent) follows from two observations. First, by convexity of P P P, no deleted value or operation could be the parent of any value not in P P P, and thus the p a r e n t parentparent relation is well-defined on G′G'G′: i m(p a r e n t)⊆O′im(parent) \subseteq O'im(parent)⊆O′. Secondly, all new values and operations added to the boundary region of G′G'G′ are from the root region of H H H, 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.10, as well as the convexity requirements of Definition 3.11 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 v v v belongs to a region that is not the top level region of the subgraph3, we can repeatedley hoist v v v 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.

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 d e f(o)\mathit\,{def}(o)de f(o) and u s e (o)\mathit{use}\,(o)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. ↩︎