Dependently-Typed Formalisation of Typed Term Graphs
We employ the dependently-typed programming language Agda2 to explore formalisation of untyped and typed term graphs directly as set-based graph structures, via the gs-monoidal categories of Corradini and Gadducci, and as nested let-expressions using Pouillard and Pottier’s NotSoFresh library of variable-binding abstractions.
💡 Research Summary
The paper presents a comprehensive formalisation of both untyped and typed term graphs using the dependently‑typed programming language Agda2. The motivation stems from the Coconut project, which generates highly optimised assembly code from “code graphs” – a variant of term graphs – currently implemented in Haskell. While Haskell’s type system can guarantee well‑typedness for manually constructed graphs, it cannot preserve type information through graph manipulation operations, forcing separate proofs or costly runtime checks. By moving to a dependently‑typed setting, the authors aim to keep type information intact throughout the entire graph lifecycle and to enable stronger correctness properties beyond mere type preservation.
The paper begins with a concise introduction to Agda’s core concepts: the hierarchy of universes (Set, Set₁, …), dependent pair types (Σ‑types), and setoids (sets equipped with an explicit equivalence relation). Setoids are crucial because many graph‑theoretic properties (e.g., isomorphism) require reasoning about equality modulo a relation rather than syntactic equality.
Untyped term graphs (DHG₁).
A directed hyper‑graph with a single output per edge is defined as a record containing:
- a setoid
Innerof non‑input nodes, - a disjoint union
Node = Fin m ⊎⊎ Innerof input positions (Fin m) and inner nodes, - a vector
output : Vec Node nof output nodes, - a setoid
Edge, - a dependent map
eInfo : Edge → Σ (k : ℕ) (Label k × Vec Node k)that records the arity, label, and input nodes of each edge, - a function
eOut : Edge → Innergiving the unique output node of an edge.
The design deliberately allows eOut to be non‑surjective (unused inner nodes) and non‑injective (join nodes). If bijectivity is required, the authors note that an inverse pair of maps can replace eOut.
Typed term graphs.
A global set Type of node types is introduced. Edge labels become indexed by vectors of input and output types: Label : {m n : ℕ} → Vec Type m → Vec Type n → Set. An EdgeType record stores the arities and the concrete type vectors. Using Σ‑types, a dependent “typed set” Typed A ty pairs an element a : A with a proof that its type equals a given ty. This yields a family of “typed vectors” where the element type depends on the position, enabling precise typing of the input and output node vectors of each edge.
Graph composition and the gs‑monoidal category.
The authors adopt the gs‑monoidal category framework of Corradini and Gadducci. Sequential composition (seqJungle) is implemented by taking the disjoint union of the inner node sets of two graphs and appropriately re‑wiring the output nodes of the first graph to the inputs of the second. Parallel composition (parJungle) similarly combines the input and output vectors using inject+ and raise to shift indices. Primitive graphs (prim) and wiring graphs (wire, idJungle, dupJungle, exchJungle) are defined as building blocks, allowing the construction of the categorical identities, duplicators, and symmetries required for a gs‑monoidal structure.
Let‑expression representation via NotSoFresh.
To bridge the graph world with a more functional syntax, the paper employs the NotSoFresh library (Pouillard & Pottier). This library provides a robust abstraction for variable binding, freshness, and scope management. By encoding term graphs as nested let‑expressions, the authors obtain a representation that is both amenable to program‑style manipulation and retains the full dependent‑type information, enabling formal proofs about substitution, sharing, and variable capture.
Key contributions and insights.
- Preservation of typing through graph transformations. By encoding graphs directly in Agda’s type system, every construction (primitive, wiring, composition) is guaranteed to respect the typing constraints encoded in the
EdgeTypeandTypedstructures. - Formal categorical semantics. The gs‑monoidal category view is not merely an abstract observation; it is realized concretely in Agda, with all required morphisms and equations provable within the language.
- Variable‑binding abstraction for graphs. The use of NotSoFresh demonstrates that term graphs can be treated as first‑class let‑expressions without sacrificing the ability to reason about them formally.
- Foundations for verified code‑generation pipelines. The work lays the groundwork for a compiler backend where each optimisation step on code graphs can be mechanically verified to preserve both structural and type correctness.
In summary, the paper showcases how dependent types can bridge the gap between high‑level functional specifications and low‑level graph‑based intermediate representations, providing a solid foundation for verified, type‑safe code generation.
Comments & Academic Discussion
Loading comments...
Leave a Comment