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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! # Arrow
//!
//! This module provides the `Arrow` trait which represents a generalized notion of computation
//! beyond ordinary functions. Arrows provide abstractions for structuring computations,
//! especially those involving pairs and independent composition.
//!
//! ## Mathematical Definition
//!
//! An arrow is a category with additional structure that allows:
//! - Lifting pure functions into the arrow type (`arrow`)
//! - Processing pairs of values (`first`, `second`)
//! - Splitting computations (`split`)
//! - Combining computations over pairs (`combine_morphisms`)
//!
//! ## Laws
//!
//! A valid arrow must satisfy these laws:
//!
//! ### Haskell to Rustica Mapping
//!
//! | Haskell | Rustica | Description |
//! |---------|---------|-------------|
//! | `arr f` | `Arrow::arrow(f)` | Lift a pure function into an arrow |
//! | `f >>> g` | `Category::compose_morphisms(&g, &f)` | Compose arrows (f first, then g) |
//! | `f *** g` | `Arrow::combine_morphisms(&f, &g)` | Apply f and g to pair components |
//! | `first f` | `Arrow::first(&f)` | Apply f to first component of pair |
//! | `second f` | `Arrow::second(&f)` | Apply f to second component of pair |
//! | `f &&& g` | `Arrow::split(&f, &g)` | Fanout: apply both f and g to input |
//!
//! ### 1. Category Laws
//!
//! ```text
//! Arrow::arrow(id) >>> f = f = f >>> Arrow::arrow(id)
//! (f >>> g) >>> h = f >>> (g >>> h)
//! ```
//!
//! In Rustica terms:
//! ```text
//! compose_morphisms(&f, &arrow(|x| x)) == f
//! compose_morphisms(&arrow(|x| x), &f) == f
//! compose_morphisms(&h, &compose_morphisms(&g, &f)) == compose_morphisms(&compose_morphisms(&h, &g), &f)
//! ```
//!
//! ### 2. Arrow Laws
//!
//! ```text
//! first (f >>> g) = first f >>> first g
//! first (arr f) = arr (f *** id)
//! first f >>> arr (id *** g) = arr (id *** g) >>> first f
//! first f >>> arr fst = arr fst >>> f
//! first (first f) >>> arr assoc = arr assoc >>> first f
//! ```
//! where `assoc ((a,b),c) = (a,(b,c))`
//!
//! ## Common Use Cases
//!
//! The Arrow trait is commonly used in scenarios where:
//!
//! 1. **Pure Function Composition**
//! - Composing functions in a type-safe way
//! - Building complex transformations from simple ones
//!
//! 2. **Stateful Computations**
//! - Handling computations with context or state
//! - Managing side effects in a pure way
//!
//! 3. **Parallel Processing**
//! - Splitting computations into parallel paths
//! - Combining results from multiple computations
//!
//! 4. **Stream Processing**
//! - Processing data streams with rich operations
//! - Composing stream transformations
//!
//! ## Relationship with Other Functional Traits
//!
//! - **Category**: Arrow extends Category with operations for structuring computations.
//!
//! - **Monad**: Arrows are more general than monads in some ways and more restricted in others.
//! Every monad gives rise to a Kleisli arrow, but not every arrow comes from a monad.
//!
//! - **Applicative**: Arrows can express applicative computations but with a different interface.
use crateCategory;
/// A trait representing arrows in category theory, which generalizes computation from regular functions
/// to more sophisticated notions of computation.
///
/// # Mathematical Definition
///
/// An arrow is a category with additional structure that allows:
/// 1. Lifting pure functions into the arrow type
/// 2. Processing pairs of values
/// 3. Fanout (splitting) computations
///
/// # Laws
///
/// An arrow must satisfy these laws:
///
/// 1. Category Laws:
/// ```text
/// arrow id >>> f = f = f >>> arrow id
/// (f >>> g) >>> h = f >>> (g >>> h)
/// ```
///
/// 2. Arrow Laws:
/// ```text
/// first (f >>> g) = first f >>> first g
/// first (arr f) = arr (f *** id)
/// first f >>> arr (id *** g) = arr (id *** g) >>> first f
/// first f >>> arr fst = arr fst >>> f
/// first (first f) >>> arr assoc = arr assoc >>> first f
/// ```
/// where `assoc ((a,b),c) = (a,(b,c))`
///
/// # Type Parameters
///
/// * `B`, `C`, `D`, `E`: Type parameters for the various morphisms
/// * `F`: Function type that can be lifted into an arrow
///
/// # Common Use Cases
///
/// 1. **Pure Function Composition**
/// - Composing functions in a type-safe way
/// - Building complex transformations from simple ones
///
/// 2. **Stateful Computations**
/// - Handling computations with context or state
/// - Managing side effects in a pure way
///
/// 3. **Parallel Processing**
/// - Splitting computations into parallel paths
/// - Combining results from multiple computations
///
/// 4. **Stream Processing**
/// - Processing data streams with rich operations
/// - Composing stream transformations