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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! # Comonad
//!
//! This module provides the `Comonad` trait which represents the categorical dual of a monad.
//! While monads wrap values in a context and sequence computations that produce values,
//! comonads extract values from a context and sequence computations that consume contexts.
//!
//! # Category Theory
//!
//! In category theory, the `extract` operation must be total (always succeed). This means
//! types like `Option` and `Result` are not valid comonads because `extract` cannot be
//! safely implemented for `None` or `Err` cases.
//!
//! ## Examples
//!
//! The `Comonad` trait enables powerful patterns for working with contextualized values:
//!
//! ```rust
//! use rustica::traits::comonad::Comonad;
//! use rustica::datatypes::id::Id;
//!
//! // Using Id<T> as a comonad (always safe)
//! let value: Id<i32> = Id::new(42);
//!
//! // Extract the value from the context (always succeeds)
//! assert_eq!(value.extract(), 42);
//!
//! // Extend a computation over the context
//! let doubled = value.extend(|id| id.extract() * 2);
//! assert_eq!(doubled.extract(), 84);
//!
//! // Using nested comonadic operations
//! let result = value
//! .extend(|id| id.extract() * 3) // multiply by 3
//! .extend(|id| id.extract() + 10); // add 10 to the result
//! assert_eq!(result.extract(), 136);
//! ```
//!
//! ## Mathematical Definition
//!
//! In category theory, a comonad on a category C consists of:
//! - An endofunctor W: C → C
//! - A natural transformation ε: W → Id (called `extract`) - **MUST BE TOTAL**
//! - A natural transformation δ: W → W² (called `duplicate`)
//!
//! ## Laws
//!
//! A valid comonad must satisfy these laws:
//!
//! 1. **Counit Law**: `extract(w.extend(f)) = f(w)`
//! Extracting from an extended comonad equals applying the function directly.
//!
//! 2. **Identity (observational)**: `extract(w.extend(|x| x.extract())) = extract(w)`
//! Extending with a function that extracts should not change the extracted value.
//!
//! ## Totality Requirement
//!
//! The `extract` operation must be total (never fail). This is a fundamental requirement
//! of category theory. Types where extraction can fail (like `Option` or `Result`) are
//! not valid comonads.
//!
//! ## Common Use Cases
//!
//! Comonads are particularly useful for:
//!
//! 1. **Data with Context** - When you need to process values while considering their surroundings
//! (e.g., cellular automata, image processing with neighborhoods)
//!
//! 2. **Stateful Transformations** - When transformations depend on the current state
//! (e.g., traced computations, store-based computations)
//!
//! 3. **Non-empty Structures** - When you need guaranteed extraction from collections
//! (e.g., non-empty lists, streams, zippers)
//!
//! 4. **UI Components** - When components need access to their environment/context
//! (e.g., React-like component trees with context)
//!
//! ## Safe Comonad Types
//!
//! Examples of types that can safely implement Comonad:
//! - `Id<T>` (Identity)
//! - `NonEmpty<Vec<T>>` (Non-empty vectors)
//! - `Stream<T>` (Infinite streams)
//! - `Zipper<T>` (List zippers with focus)
//! - `Store<S, A>` (Store comonad)
//! - `Traced<M, A>` where M is a monoid
//!
//! ## Relationship with Other Functional Traits
//!
//! - **Monad**: Comonads are the categorical dual of monads. While monads let you sequence
//! computations that produce contexts, comonads let you sequence computations that consume contexts.
//!
//! - **Functor**: Both monads and comonads are functors. Comonads add the ability to extract
//! values from and duplicate contexts.
//!
use crateFunctor;
/// A comonad is the categorical dual of a monad, providing operations to extract values from a context
/// and extend computations that consume contexts. While monads represent computations that add context,
/// comonads represent computations that can read from contexts.
///
/// # Category Theory Requirements
///
/// For a type to be a valid comonad, the `extract` operation must be total (always succeed).
/// This means types like `Option<T>` and `Result<T, E>` are NOT valid comonads because
/// `extract` cannot be safely implemented for `None` or `Err` cases.
///
/// Valid comonads include:
/// - `Id<T>` (Identity)
/// - Non-empty lists
/// - Streams
/// - Zippers
/// - Store comonads
///
/// Note: Comonads are independent of monads in category theory. They both extend Functor,
/// but a type can be a comonad without being a monad, and vice versa.