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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! # Category
//!
//! This module provides the `Category` trait which represents a category in the sense of
//! category theory. Categories are a fundamental mathematical structure that help model
//! and organize various types of objects and the mappings between them.
//!
//! ## Mathematical Definition
//!
//! A category consists of:
//! - A collection of objects
//! - A collection of morphisms (arrows) between objects
//! - An identity morphism for each object
//! - A composition operation for morphisms
//!
//! **Note on Rust Implementation Limitations:**
//! In pure category theory, a category explicitly maintains a collection of objects.
//! However, in Rust's type system, objects are represented implicitly through the
//! type parameters of morphisms. This is a necessary compromise due to Rust's
//! static typing and the difficulty of representing arbitrary object collections
//! at the type level.
//!
//! ## Laws
//!
//! A valid category must satisfy these laws:
//!
//! 1. Identity:
//! ```text
//! f ∘ id = f = id ∘ f
//! ```
//! Composing any morphism with identity leaves it unchanged.
//!
//! 2. Associativity:
//! ```text
//! f ∘ (g ∘ h) = (f ∘ g) ∘ h
//! ```
//! The order of composition doesn't matter.
//!
//! ## Note on Composition Order
//!
//! In this crate, `compose_morphisms(g, f)` represents the mathematical composition `g ∘ f`.
//! That is, `f` is applied first, then `g`.
//!
//! ## Common Use Cases
//!
//! The Category trait is commonly used in scenarios where:
//!
//! 1. **Function Composition**
//! - Representing pure functions and their composition
//! - Building complex transformations from simple ones
//!
//! 2. **Type-safe Transformations**
//! - Ensuring type safety in data transformations
//! - Modeling data flow between different types
//!
//! 3. **Abstract Algebra**
//! - Implementing mathematical structures
//! - Defining algebraic data types
//!
//! ## Relationship with Other Functional Traits
//!
//! - **Functor**: Functors are mappings between categories that preserve structure.
//! They build on the concept of categories.
//!
//! - **Monad**: Monads are a specific kind of functor with additional operations that
//! follow certain laws. They can be seen as structures within a category.
/// A trait representing a category in category theory.
///
/// # Mathematical Definition
///
/// A category consists of:
/// 1. A collection of objects (types in our case)
/// 2. A collection of morphisms (functions) between objects
/// 3. A composition operation for morphisms
/// 4. An identity morphism for each object
///
/// # Laws
///
/// A category must satisfy these laws:
///
/// 1. Identity:
/// ```text
/// f ∘ id = f = id ∘ f
/// ```
/// Composing any morphism with identity leaves it unchanged.
///
/// 2. Associativity:
/// ```text
/// f ∘ (g ∘ h) = (f ∘ g) ∘ h
/// ```
/// The order of composition doesn't matter.
///
/// # Type Parameters
///
/// * `T`: The source type of the morphism
/// * `U`: The target type of the morphism
///
/// # Common Use Cases
///
/// 1. **Function Composition**
/// - Representing pure functions and their composition
/// - Building complex transformations from simple ones
///
/// 2. **Type-safe Transformations**
/// - Ensuring type safety in data transformations
/// - Modeling data flow between different types
///
/// 3. **Abstract Algebra**
/// - Implementing mathematical structures
/// - Defining algebraic data types