1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! # Category Theory Primitives
//!
//! This module provides a basic trait, [`Category`], for representing abstract
//! categories in mathematics. A category consists of objects and morphisms
//! (or arrows) between these objects.
//!
//! ## Mathematical Definition
//!
//! A category $\mathcal{C}$ consists of:
//! - A collection of **objects**, denoted $\text{ob}(\mathcal{C})$.
//! - For every pair of objects $A, B \in \text{ob}(\mathcal{C})$, a collection of **morphisms** (or
//! arrows) from $A$ to $B$, denoted $\text{Hom}\_{\mathcal{C}}(A, B)$. If $f \in
//! \text{Hom}\_{\mathcal{C}}(A, B)$, we write $f: A \to B$.
//! - For every object $A \in \text{ob}(\mathcal{C})$, an **identity morphism** $\text{id}\_A: A \to
//! A$.
//! - For every triple of objects $A, B, C \in \text{ob}(\mathcal{C})$, a binary operation called
//! **composition of morphisms**, $ \circ : \text{Hom}\_{\mathcal{C}}(B, C) \times
//! \text{Hom}\_{\mathcal{C}}(A, B) \to \text{Hom}_{\mathcal{C}}(A, C)$. Given $g: B \to C$ and
//! $f: A \to B$, their composition is written $g \circ f: A \to C$.
//!
//! These components must satisfy two axioms:
//! 1. **Associativity**: For any morphisms $f: A \to B$, $g: B \to C$, and $h: C \to D$, the
//! equation $h \circ (g \circ f) = (h \circ g) \circ f$ must hold.
//! 2. **Identity**: For any morphism $f: A \to B$, the equations $\text{id}_B \circ f = f$ and $f
//! \circ \text{id}_A = f$ must hold.
//!
//! In this module, the objects of the category are represented by types that implement
//! the [`Category`] trait itself. The morphisms are represented by an associated type
//! [`Category::Morphism`].
// TODO (autoparallel): It may be smarter to have these use references instead of
// ownership. This way we can avoid unnecessary cloning.
/// Represents an object in a category, along with its morphisms and operations.
///
/// In category theory, a category consists of objects and morphisms between them.
/// This trait models an object within such a category. The type implementing `Category`
/// acts as an object, and it defines an associated type `Morphism` for the arrows.
///
/// The trait provides methods for morphism composition, obtaining identity morphisms,
/// and applying a morphism to an object (which can be thought of as evaluating a function
/// if objects are sets and morphisms are functions, or as a more abstract action).