open_hypergraphs/
lib.rs

1//! # Open Hypergraphs
2//!
3//! `open-hypergraphs` is a [GPU-accelerated](#data-parallelism) implementation of the
4//! [OpenHypergraph](crate::open_hypergraph::OpenHypergraph)
5//! datastructure from the paper
6//! ["Data-Parallel Algorithms for String Diagrams"](https://arxiv.org/pdf/2305.01041).
7//! Open hypergraphs are used for representing, evaluating, and differentiating large networks of operations with multiple
8//! inputs and outputs.
9//!
10//! Here's a drawing of an open hypergraph with labeled nodes `●` and hyperedges `□`.
11//!
12//! ```text
13//!                    /───────────────────────────────────   x
14//!                   ╱
15//!   x   ───────────●
16//!                 i8\      ┌─────┐
17//!                    \─────┤     │        ┌─────┐
18//!            2             │ Sub ├───●────┤ Neg ├───●───    -(x - y)
19//!   y   ─────●─────────────┤     │  i8    └─────┘  i8
20//!           i8             └─────┘
21//! ```
22//!
23//! This open hypergraph represents a circuit with two inputs, `x` and `y`.
24//! this circuit computes `x` on its first output and `- (x - y)` on its second.
25//! (The input/output labels `x`, `y`, and `-(x - y)` are only illustrative, and not part of the
26//! datastructure.)
27//!
28//! <div class="warning">
29//! Note carefully: in contrast to typical graph-based syntax representations,
30//! operations correspond to hyperedges,
31//! and values correspond to nodes!
32//! This is why nodes are labeled with types like i8 and hyperedges with operations like
33//! Sub.
34//! </div>
35//!
36//! See the [datastructure](#datastructure) section for a formal definition.
37//!
38//! # What are Open Hypergraphs For?
39//!
40//! Open Hypergraphs are a general, differentiable and data-parallel datastructure for *syntax*.
41//! Here's a few examples of suitable uses:
42//!
43//! - Differentiable array programs for deep learning in [catgrad](https://catgrad.com)
44//! - Terms in [first order logic](https://arxiv.org/pdf/2401.07055)
45//! - Programs in the [λ-calculus](https://en.wikipedia.org/wiki/Cartesian_closed_category)
46//! - [Circuits with feedback](https://arxiv.org/pdf/2201.10456)
47//! - [Interaction nets](https://dl.acm.org/doi/10.1006/inco.1997.2643)
48//!
49//! Open Hypergraphs have some unique advantages compared to tree-based representations of syntax.
50//! For example, they can represent operations with *multiple outputs*, and structures with
51//! *feedback*.
52//! See the [comparison to trees and graphs](#comparison-to-trees-and-graphs) for more detail.
53//!
54//! Differentiability of open hypergraphs (as used in [catgrad](https://catgrad.com))
55//! comes from the [data-parallel algorithm](crate::functor::optic::Optic) for generalised
56//! ahead-of-time automatic differentiation by optic composition.
57//! This algorithm is actually more general than just differentiability: read more in the papers
58//! ["Categorical Foundations of Gradient-Based Learning"](https://arxiv.org/abs/2103.01931)
59//! and ["Data-Parallel Algorithms for String Diagrams"](https://arxiv.org/pdf/2305.01041).
60//! See the [Theory](#theory) section for more pointers.
61//!
62//! # Usage
63//!
64//! If you're new to the library, you should start with the [`crate::lax`] module.
65//! This provides a mutable, imperative, single-threaded interface to building open hypergraphs
66//! which should be familiar if you've used a graph library before.
67//!
68//! We can build the example open hypergraph above as follows:
69//!
70//! ```rust
71//! use open_hypergraphs::lax::*;
72//!
73//! pub enum NodeLabel { I8 };
74//! pub enum EdgeLabel { Sub, Neg };
75//!
76//! #[test]
77//! fn build() -> OpenHypergraph<NodeLabel, EdgeLabel> {
78//!     use NodeLabel::*;
79//!     use EdgeLabel::*;
80//!
81//!     // Create an empty OpenHypergraph.
82//!     let mut example = OpenHypergraph::<NodeLabel, EdgeLabel>::empty();
83//!
84//!     // Create all 4 nodes
85//!     let x = example.new_node(I8);
86//!     let a = example.new_node(I8);
87//!     let y = example.new_node(I8);
88//!     let z = example.new_node(I8);
89//!
90//!     // Add the "Sub" hyperedge with source nodes `[x, y]` and targets `[a]`
91//!     example.new_edge(Sub, Hyperedge { sources: vec![x, y], targets: vec![a] });
92//!
93//!     // Add the 'Neg' hyperedge with sources `[a]` and targets `[z]`
94//!     example.new_edge(Neg, Hyperedge { sources: vec![a], targets: vec![z] });
95//!
96//!     // set the sources and targets of the example
97//!     example.sources = vec![x, y];
98//!     example.targets = vec![x, z];
99//!
100//!     // return the example
101//!     example
102//! }
103//! ```
104//!
105//! The [`crate::lax::var::Var`] struct is a helper on top of the imperative interface which
106//! reduces some boilerplate, especially when operators are involved.
107//! We can rewrite the above example as follows:
108//!
109//! ```ignore
110//! pub fn example() {
111//!     let state = OpenHypergraph::empty();
112//!     let x = Var::new(state, I8);
113//!     let y = Var::new(state, I8);
114//!     let (z0, z1) = (x.clone(), -(x - y));
115//! }
116//! ```
117//!
118//! See `examples/adder.rs` for a more complete example using this interface to build an n-bit full
119//! adder from half-adder circuits.
120//!
121//! # Datastructure
122//!
123//! Before giving the formal definition, let's revisit the example above.
124//!
125//! ```text
126//!                  /───────────────────────────────────
127//!                0╱
128//!     ───────────●
129//!               i8\      ┌─────┐
130//!                  \─────┤     │   1    ┌─────┐   3
131//!          2             │ Sub ├───●────┤ Neg ├───●───
132//!     ─────●─────────────┤     │  i8    └─────┘  i8
133//!         i8             └─────┘
134//! ```
135//!
136//! There are 4 nodes in this open hypergraph, depicted as `●` with a label `i8` and a
137//! node ID in the set `{0..3}`.
138//! There are two hyperedges depicted as a boxes labeled `Sub` and `Neg`.
139//!
140//! Each hyperedge has an *ordered list* of sources and targets.
141//! For example, the `Sub` edge has sources `[0, 2]` and targets `[1]`,
142//! while `Neg` has sources `[1]` and targets `[3]`.
143//! Note: the order is important!
144//! Without it, we couldn't represent non-commutative operations like `Sub`.
145//!
146//! As well as the sources and targets for each *hyperedge*, the whole "open hypergraph" also has
147//! sources and targets.
148//! These are drawn as dangling wires on the left and right.
149//! In this example, the sources are `[0, 2]`, and the targets are `[0, 3]`.
150//!
151//! <div class="warning">
152//! There are no restrictions on how many times a node can appear as a source or target of both
153//! hyperedges and the open hypergraph as a whole.
154//! </div>
155//!
156//! For example, node `0` is a source and target of the open hypergraph, *and* a source of the
157//! `Sub` edge.
158//! Another example: node `1` is not a source or target of the open hypergraph, although it *is* a
159//! target of the `Sub` hyperedge and a source of the `Neg` hyperedge.
160//!
161//! It's also possible to have nodes which are neither sources nor targets of the open hypergraph
162//! *or* any hyperedge, but that isn't pictured here. See the [theory](#theory) section for more
163//! detail.
164//!
165//! # Formal Definition
166//!
167//! Formally, an open hypergraph is a triple of:
168//!
169//! 1. A Hypergraph `h` with `N ∈ ℕ` nodes
170//! 2. An array `s` of length `A ∈ ℕ` whose elements `s_i ∈ {0..N-1}` are nodes
171//! 3. An array `t` of length `B ∈ ℕ` whose elements `t_i ∈ {0..N-1}` are nodes
172//!
173//! Many different kinds of [Hypergraph](https://en.wikipedia.org/wiki/Hypergraph) exist,
174//! but an *open* hypergraph uses a specific kind of directed hypergraph, which has:
175//!
176//! - A finite set of `N` nodes, labeled with an element from a set `Σ₀`
177//! - A finite set of `E` *hyperedges*, labeled from the set `Σ₁`
178//! - For each hyperedge `e ∈ E`,
179//!   - An ordered array of *source nodes*
180//!   - An ordered array of *target nodes*
181//!
182//! # Comparison to Trees and Graphs
183//!
184//! Let's compare the open hypergraph representation of the example term above against *tree* and
185//! *graph* representations.
186//!
187//! When considered as a tree, the term `(x, - (x - y))` can be drawn as follows:
188//!
189//! ```text
190//!         Pair
191//!        /    \
192//!       /      Neg
193//!      x        |
194//!              Sub
195//!             /   \
196//!            x     y
197//! ```
198//!
199//! There are two problems here:
200//!
201//! 1. To handle multiple outputs, we had to include a tuple constructor "Pair" in our language.
202//!    This means we'd also need to add other functions to deal with pairs, "polluting" the base
203//!    language.
204//! 2. The "sharing" of variables is not evident from the tree structure: x is used twice, but we
205//!    have to compare strings to "discover" that fact.
206//!
207//! In contrast, the open hypergraph:
208//!
209//! 1. Allows for terms with **multiple outputs**, without having to introduce a tuple type to the
210//!    language.
211//! 2. Encodes the **sharing** of variables naturally by allowing nodes to appear in multiple
212//!    sources and targets.
213//!
214//! Another common approach is to use a *graph* for syntax where nodes are operations, and an edge
215//! between two nodes indicates the *output* of the source node is the *input* of the target.
216//! Problems:
217//!
218//! 1. Nodes don't distinguish the order of edges, so argument order has to be tracked separately
219//! 2. There is no notion of input or output to the whole system.
220//!
221//! In contrast, the open hypergraph:
222//!
223//! 1. Naturally handles operations with multiple ordered inputs and outputs (as *hyperedges*)
224//! 2. Comes equipped with global source and target nodes
225//!
226//! Open Hypergraphs have general utility because they model any system which can be described in terms of symmetric monoidal
227//! categories.
228//! Some examples are listed [above](#what-are-open-hypergraphs-for);
229//! see the [Theory](#theory) section for more pointers to detail on the mathematical
230//! underpinnings.
231//!
232//! # Theory
233//!
234//! Formally, an `OpenHypergraph<Σ₀, Σ₁>` is an arrow of
235//! the free [symmetric monoidal category](https://en.wikipedia.org/wiki/Symmetric_monoidal_category)
236//! presented by the signature `(Σ₀, Σ₁)` plus "Special Frobenius" structure.
237//!
238//! This extra structure is sometimes useful (e.g. in autodiff), but can be removed by restricting
239//! the open hypergraph such that nodes always appear in exactly one source and target.
240//! This condition is called "monogamous acyclicity".
241//!
242//! A complete mathematical explanation can be found in the papers
243//! [String Diagram Rewrite Theory I](https://arxiv.org/abs/2012.01847),
244//! [II](https://arxiv.org/abs/2104.14686),
245//! and
246//! [III](https://arxiv.org/abs/2109.06049),
247//! which also includes details on how to *rewrite* open hypergraphs.
248//!
249//! The implementation in *this* library is based on the data-parallel algorithms described in the
250//! paper [Data Parallel Algorithms for String Diagrams](https://arxiv.org/pdf/2305.01041).
251//! In particular, the "generalised autodiff" algorithm can be found in Section 10 ("Optic
252//! Composition using Frobenius Structure") of that paper.
253
254pub mod array;
255pub mod category;
256pub mod finite_function;
257pub mod indexed_coproduct;
258pub mod operations;
259pub mod semifinite;
260
261pub mod hypergraph;
262pub mod open_hypergraph;
263
264pub mod eval;
265pub mod functor;
266pub mod layer;
267
268// imperative interface to building open hypergraphs
269pub mod lax;
270
271pub mod prelude {
272    //! Type alises for Open Hypergraphs using the [`VecKind`] array backend.
273    pub use crate::array::vec::*;
274    pub use crate::category::*;
275
276    pub type OpenHypergraph<Obj, Arr> = crate::open_hypergraph::OpenHypergraph<VecKind, Obj, Arr>;
277    pub type Hypergraph<Obj, Arr> = crate::hypergraph::Hypergraph<VecKind, Obj, Arr>;
278    pub type FiniteFunction = crate::finite_function::FiniteFunction<VecKind>;
279    pub type SemifiniteFunction<T> = crate::semifinite::SemifiniteFunction<VecKind, T>;
280    pub type IndexedCoproduct<F> = crate::indexed_coproduct::IndexedCoproduct<VecKind, F>;
281}