This note packages the mathematical backbone of the MetaGraph project. It makes the “graphs all the way down” slogan precise, shows how standard algebraic machinery applies, and cross-checks the theory against the engineering design captured in
docs/architecture.md.
Fix a universe of payloads
-
Atoms. For any
$p \in P$ , the object$Atom(p)$ belongs to$\mathrm{RMG}$ . -
Graphs. Let
$S = (V, E, s, t)$ be a finite directed multigraph (“1-skeleton”), and let
assign a meta-graph to each vertex and edge respectively. The triple
Minimality ensures well-foundedness: every RMG is built by finitely many applications of these constructors, bottoming out at atoms. In practice, atoms correspond to graph chunks whose node_count = edge_count = 0 with payload stored in the bundle’s blob region.
Let
An RMG is an element of the initial algebra
-
Structural recursion (catamorphisms). For any
$F$ -algebra$\varphi : F(X) \to X$ , there exists a unique homomorphism$fold_\varphi : \mathrm{RMG} \to X$ defined by
This is the semantic foundation for metagraph_fold(...) style APIs.
-
Induction principle. Properties
$\mathcal{P}$ on$\mathrm{RMG}$ can be shown by checking$\mathcal{P}$ on atoms and assuming it holds for each attachment when proving it for$(S,\alpha,\beta)$ . -
Coinduction (optional). Replacing
$\mu F$ by the greatest fixed point$\nu F$ admits possibly infinite unfoldings, useful for live streaming or netlists with recursion.
A morphism
- A graph homomorphism on 1-skeletons
$f_V : V \to V'$ ,$f_E : E \to E'$ with$s' \circ f_E = f_V \circ s$ and$t' \circ f_E = f_V \circ t$ . - Compatible meta-morphisms
$f_v : \alpha(v) \to \alpha'(f_V(v))$ and$f_e : \beta(e) \to \beta'(f_E(e))$ , defined recursively.
With identity and composition defined pointwise,
-
Depth.
$depth(Atom(p)) = 0$ , and
-
$k$ -unfolding. Define$[ G ]_k$ by recursively replacing attachments up to depth$k$ with their 1-skeletons. The filtered colimit$\varinjlim_k [ G ]_k$ is the infinite unfolding used for analysis or visualization. - Hylomorphisms / paramorphisms. Standard recursion schemes (build-then-fold, folds with access to subterms) are inherited from the initial algebra semantics.
The engineering design relies on constructive graph rewrites (e.g. inlining a node’s attachment). Formally, we work in an adhesive category where DPO (double-pushout) rewriting is defined. Concretely:
- Extract the 1-skeleton pattern
$L \hookrightarrow G$ to replace. - Use the attachment pointers to identify the interface.
- Perform the pushout to splice in the replacement RMG.
Because each attachment is itself an RMG, rewrites recurse automatically: replacing a vertex or edge is equivalent to replacing its attached subgraph and updating offsets. The chunk layout in the bundle (node/edge tables, edge_data_idx) is precisely the data needed to compute these pushouts in
| Mathematical Notion | On-Disk Representation | Loader Behaviour |
|---|---|---|
| Shape |
Graph chunk header (node_count, edge_count) + node/edge index arrays |
Hydration reconstructs adjacency and offsets |
| Vertex attachment |
Entry in node index referencing another graph chunk |
mg_graph_hydrate_node(...) follows offset into mapped region |
| Edge attachment |
edge_data_idx (graph chunk containing semantics/pipeline) |
mg_graph_hydrate_edge(...) pulls edge-graph lazily |
| Atom |
node_count = edge_count = 0, payload stored as blob/string |
Treated as leaf: hydration returns zero-degree graph |
| Fold |
metagraph_fold(...) style API |
Provided callback |
| Pushout splice | In-place pointer rewrite + integrity update | Implements DPO rewrite guaranteeing consistency |
This table ensures that the theoretical constructors map one-to-one to concrete chunk types and loader operations. No “extra” structures appear in code that lack a slot in the mathematics, and vice versa.
- Universal property. The initial algebra ensures uniqueness of interpreters. Any evaluation or analysis pass must map
AtomandGraphconstructors; the fold generated is unique. - Compositionality. The (strict symmetric) monoidal structure inherited from graph composition allows parallel composition and nesting to commute with rewrites (useful for concurrency).
- Expressiveness. Every RMG simultaneously generalizes node-replacement, edge-replacement, and hierarchical graph models—enabling nodes and edges to store semantics without losing regularity.
- Rewrite safety. Adhesivity plus DPO rewriting provide confluence results and critical pair analysis, guiding safe live edits or builder transforms.
- Homotopy-flavoured reasoning. Execution traces of rewrites form higher-dimensional paths; this opens the door to exploiting homotopy type theory or infinity-groupoid interpretations for proof-carrying pipelines.
Sections §4 and §5 of docs/architecture.md describe arenas, chunk packets, hydration, and integrity checks. The set-theoretic constructors here align exactly: atoms ↔ leaf chunks,
- Engelfriet & Rozenberg, Node Replacement Graph Grammars.
- Drewes et al., Hyperedge Replacement Graph Grammars.
- Lack & Sobociński, Adhesive Categories.
- Fong, Decorated Cospans.
- OpenCog / Hyperon Atomspace documentation (metagraph rewriting in practice).