open_hypergraphs/lax/
open_hypergraph.rs

1//! Cospans of Hypergraphs.
2use super::hypergraph::*;
3use crate::array::vec::VecKind;
4
5/// A lax OpenHypergraph is a cospan of lax hypergraphs:
6/// a hypergraph equipped with two finite maps representing the *interfaces*.
7#[derive(Debug, Clone)]
8pub struct OpenHypergraph<O, A> {
9    pub sources: Vec<NodeId>,
10    pub targets: Vec<NodeId>,
11    pub hypergraph: Hypergraph<O, A>,
12}
13
14// Imperative-specific methods
15impl<O, A> OpenHypergraph<O, A> {
16    /// The empty OpenHypergraph with no nodes and no edges.
17    ///
18    /// In categorical terms, this is the identity map at the unit object.
19    pub fn empty() -> Self {
20        OpenHypergraph {
21            sources: vec![],
22            targets: vec![],
23            hypergraph: Hypergraph::empty(),
24        }
25    }
26
27    pub fn from_strict(f: crate::open_hypergraph::OpenHypergraph<VecKind, O, A>) -> Self {
28        let sources = f.s.table.0.into_iter().map(NodeId).collect();
29        let targets = f.t.table.0.into_iter().map(NodeId).collect();
30        let hypergraph = Hypergraph::from_strict(f.h);
31        OpenHypergraph {
32            sources,
33            targets,
34            hypergraph,
35        }
36    }
37
38    /// Create a new node in the hypergraph labeled `w`.
39    pub fn new_node(&mut self, w: O) -> NodeId {
40        self.hypergraph.new_node(w)
41    }
42
43    pub fn new_edge(&mut self, x: A, interface: Hyperedge) -> EdgeId {
44        self.hypergraph.new_edge(x, interface)
45    }
46
47    /// Create a new "operation" in the hypergraph.
48    /// Concretely, `f.new_operation(x, s, t)` mutates `f` by adding:
49    ///
50    /// 1. a new hyperedge labeled `x`
51    /// 2. `len(s)` new nodes, with the `i`th node labeled `s[i]`
52    /// 3. `len(t)` new nodes, with the `i`th node labeled `t[i]`
53    ///
54    /// Returns the new hyperedge ID and the [`NodeId`]s of the source/target nodes.
55    ///
56    /// This is a convenience wrapper for [`Hypergraph::new_operation`]
57    pub fn new_operation(
58        &mut self,
59        x: A,
60        source_type: Vec<O>,
61        target_type: Vec<O>,
62    ) -> (EdgeId, Interface) {
63        self.hypergraph.new_operation(x, source_type, target_type)
64    }
65
66    /// Compute an open hypergraph by calling `to_hypergraph` on the internal `Hypergraph`.
67    pub fn unify(&mut self, v: NodeId, w: NodeId) {
68        self.hypergraph.unify(v, w);
69    }
70
71    pub fn add_edge_source(&mut self, edge_id: EdgeId, w: O) -> NodeId {
72        self.hypergraph.add_edge_source(edge_id, w)
73    }
74
75    pub fn add_edge_target(&mut self, edge_id: EdgeId, w: O) -> NodeId {
76        self.hypergraph.add_edge_target(edge_id, w)
77    }
78}
79
80impl<O: Clone + PartialEq, A: Clone + PartialEq> OpenHypergraph<O, A> {
81    /// Apply the quotient map to identify nodes in the internal [`Hypergraph`].
82    /// This deletes the internal quotient map, resulting in a *strict* [`OpenHypergraph`].
83    pub fn quotient(&mut self) {
84        // mutably quotient self.hypergraph, returning the coequalizer q
85        let q = self.hypergraph.quotient();
86
87        // note: this is composition of finite functions `q >> self.sources`,
88        // but we do it mutably in-place.
89        self.sources
90            .iter_mut()
91            .for_each(|x| *x = NodeId(q.table[x.0]));
92        self.targets
93            .iter_mut()
94            .for_each(|x| *x = NodeId(q.table[x.0]));
95    }
96
97    /// Convert this *lax* [`OpenHypergraph`] to a strict [`crate::prelude::OpenHypergraph`] by
98    /// quotienting.
99    pub fn to_open_hypergraph(mut self) -> crate::prelude::OpenHypergraph<O, A> {
100        use crate::array::vec::VecArray;
101        use crate::finite_function::FiniteFunction;
102        use crate::open_hypergraph::OpenHypergraph;
103
104        self.quotient();
105
106        let target = self.hypergraph.nodes.len();
107
108        let s = {
109            let table = self.sources.iter().map(|x| x.0).collect();
110            FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
111        };
112
113        let t = {
114            let table = self.targets.iter().map(|x| x.0).collect();
115            FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
116        };
117
118        let h = self.hypergraph.to_hypergraph();
119
120        OpenHypergraph::new(s, t, h).expect("any valid lax::Hypergraph must be quotientable!")
121    }
122}