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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! The `HKT` trait enables emulation of higher-kinded types in Rust.
//!
//! Higher-kinded types (HKTs) are type constructors that take a type and return
//! another type. Rust does not directly support higher-kinded types, but we can
//! emulate them using associated types.
//!
//! This module provides the `HKT` trait and related traits that form the
//! foundation for higher-kinded polymorphism in the Rustica library.
//!
//! ## Limitations of HKT Simulation in Rust
//!
//! While this implementation provides a workable approximation of higher-kinded types,
//! it has several important limitations compared to true HKT support:
//!
//! ### 1. **Associated Type Constraints**
//! - Each type constructor must explicitly implement the trait (typically once per wrapper type)
//! - Cannot express arbitrary type constructors at the type level
//! - Limited composability compared to true HKT systems
//!
//! ### 2. **Inference Limitations**
//! - Type inference often requires explicit type annotations
//! - Complex generic bounds can become unwieldy
//! - Some mathematically valid operations cannot be expressed
//!
//! ### 3. **Runtime Overhead**
//! - HKT emulation can lead to more complex bounds and less ergonomic APIs
//! - Some patterns may require trait objects or boxing (depending on the abstraction)
//! - Performance is typically still monomorphized, but designs built on HKT-style traits can
//! encourage more abstraction layers
//!
//! ### 4. **Expressiveness Gaps**
//! - Cannot represent some category theory concepts directly
//! - Limited support for type-level computation
//! - Some functor laws cannot be verified at compile time
//!
//! ### 5. **Ergonomics Issues**
//! - Verbose syntax for complex type relationships
//! - Difficult to write generic code over multiple HKT instances
//! - Error messages can be cryptic and hard to debug
//!
//! Despite these limitations, this HKT simulation provides a practical foundation
//! for functional programming patterns in Rust while maintaining type safety.
//!
//! # Examples
//!
//! ```rust
//! use rustica::traits::hkt::HKT;
//!
//! // Define our own wrapper types for the examples
//! // MyOption is a simple wrapper around Option
//! #[derive(Clone)]
//! struct MyOption<T>(Option<T>);
//!
//! // Implement HKT for our custom wrapper
//! impl<T> HKT for MyOption<T> {
//! type Source = T;
//! type Output<U> = MyOption<U>;
//! }
//!
//! // MyVec is a simple wrapper around Vec
//! #[derive(Clone)]
//! struct MyVec<T>(Vec<T>);
//!
//! // Implement HKT for our custom wrapper
//! impl<T> HKT for MyVec<T> {
//! type Source = T;
//! type Output<U> = MyVec<U>;
//! }
//!
//! // Writing a function that works with any HKT
//! fn map_hkt<H, T, U, F>(value: &H, f: F) -> H::Output<U>
//! where
//! H: HKT<Source = T>,
//! F: Fn(&T) -> U,
//! {
//! // This would be the implementation in a real case
//! // Here we're just demonstrating the type signatures
//! unimplemented!()
//! }
//! ```
use PhantomData;
/// A trait for types that can be treated as higher-kinded types.
///
/// In category theory, a functor is a mapping between categories. In Rust terms,
/// it can be seen as a container type that can be transformed while preserving
/// its structure.
///
/// The `HKT` trait provides a way to refer to the contained type and to construct
/// the same container with a different contained type.
///
/// # Type Parameters
///
/// * `Source` - The type contained in this HKT
/// * `Output<U>` - The same HKT but containing type U instead of Source
///
/// # Examples
///
/// ```rust
/// use rustica::traits::hkt::HKT;
///
/// // Define our own wrapper types for the examples
/// // MyOption is a simple wrapper around Option
/// #[derive(Clone)]
/// struct MyOption<T>(Option<T>);
///
/// // Implement HKT for our custom wrapper
/// impl<T> HKT for MyOption<T> {
/// type Source = T;
/// type Output<U> = MyOption<U>;
/// }
///
/// // MyVec is a simple wrapper around Vec
/// #[derive(Clone)]
/// struct MyVec<T>(Vec<T>);
///
/// // Implement HKT for our custom wrapper
/// impl<T> HKT for MyVec<T> {
/// type Source = T;
/// type Output<U> = MyVec<U>;
/// }
///
/// // A function that uses HKT
/// fn transform<H, T, U>(container: &H, value: &T) -> H::Output<U>
/// where
/// H: HKT<Source = T>,
/// T: Clone,
/// U: From<T>,
/// {
/// // Just an example signature
/// unimplemented!()
/// }
/// ```
/// A trait for higher-kinded types that have two type parameters.
///
/// This trait extends the `HKT` trait to allow for types that have a second type
/// parameter, such as `Result<T, E>` or `Either<L, R>`.
///
/// # Important: Type Parameter Mapping Convention
///
/// The mapping between lexical type parameters and `Source`/`Source2` follows
/// the functional programming convention where the "success" or "right" value
/// is the primary content (mapped by `Functor::fmap`):
///
/// | Type | `Source` (primary) | `Source2` (secondary) |
/// |------|--------------------|-----------------------|
/// | `Result<T, E>` | `T` (Ok value) | `E` (Err value) |
/// | `Either<L, R>` | `R` (Right value) | `L` (Left value) |
///
/// This means for `Either<L, R>`, the lexical order (`L`, `R`) is **reversed**
/// in the HKT mapping. This is intentional and consistent with how `Either` is
/// used in functional programming (Right = success path).
///
/// # Examples
///
/// ```rust
/// use rustica::traits::hkt::{HKT, BinaryHKT};
///
/// // Define our own wrapper types for the examples
/// // MyResult is a simple wrapper around Result
/// #[derive(Clone)]
/// struct MyResult<T, E>(Result<T, E>);
///
/// // Implement HKT for our custom wrapper
/// impl<T, E> HKT for MyResult<T, E> {
/// type Source = T;
/// type Output<U> = MyResult<U, E>;
/// }
///
/// // Implement BinaryHKT for our custom wrapper
/// impl<T, E> BinaryHKT for MyResult<T, E> {
/// type Source2 = E;
/// type BinaryOutput<Type1, Type2> = MyResult<Type1, Type2>;
///
/// fn map_second<F, NewType2>(&self, f: F) -> Self::BinaryOutput<Self::Source, NewType2>
/// where
/// F: Fn(&Self::Source2) -> NewType2,
/// Self::Source: Clone,
/// Self::Source2: Clone,
/// {
/// MyResult(match &self.0 {
/// Ok(v) => Ok(v.clone()),
/// Err(e) => Err(f(e)),
/// })
/// }
///
/// fn map_second_owned<F, NewType2>(self, f: F) -> Self::BinaryOutput<Self::Source, NewType2>
/// where
/// F: Fn(Self::Source2) -> NewType2,
/// {
/// MyResult(match self.0 {
/// Ok(v) => Ok(v),
/// Err(e) => Err(f(e)),
/// })
/// }
/// }
///
/// // A function that works with BinaryHKT
/// fn map_second_generic<H, T, E, U, F>(value: &H, f: F) -> H::BinaryOutput<T, U>
/// where
/// H: BinaryHKT<Source = T, Source2 = E>,
/// F: Fn(&E) -> U,
/// T: Clone,
/// E: Clone,
/// U: Clone,
/// {
/// value.map_second(f)
/// }
/// ```