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
//! # Higher-Kinded Type (HKT) Infrastructure
//!
//! This module provides the core traits and marker types for simulating
//! Higher-Kinded Types in Rust. This allows for abstracting over type
//! constructors like `Option`, `Vec`, `Result<_, E>`, etc., which is
//! fundamental for defining generic functional programming traits like
//! `Functor`, `Applicative`, and `Monad` in a way that they can operate
//! over these different type constructors.
//!
//! The primary mechanism used is a combination of:
//! 1. A central `HKT` trait with a Generic Associated Type (GAT) `Applied<Arg>`.
//! 2. Marker structs (e.g., `OptionHKTMarker`) that implement `HKT` and specify
//! what `Applied<Arg>` resolves to for their respective type constructor.
//!
//! This setup enables traits to be generic over the *marker type*, and through
//! the marker's `Applied<Arg>` GAT, they can refer to the concrete type
//! (e.g., `Option<String>`, `Vec<i32>`).
use crate;
use PhantomData;
/// Represents a Higher-Kinded Type (HKT) constructor.
///
/// This trait is the cornerstone of the HKT simulation. It allows abstracting
/// over type constructors that take one type argument, such as `Option<_>`,
/// `Vec<_>`, or `Result<_, E>` (where `E` is fixed).
///
/// Implementors of this trait are typically lightweight "marker" structs
/// (e.g., [`OptionHKTMarker`], [`VecHKTMarker`]) that don't hold data themselves
/// but serve to identify the type constructor they represent.
// --- Marker Structs for common HKTs ---
/// Marker for the `Option` type constructor.
///
/// Implements [`HKT`] such that `OptionHKTMarker::Applied<T>` resolves to `Option<T>`.
;
/// Marker for the `Vec` type constructor.
///
/// Implements [`HKT`] such that `VecHKTMarker::Applied<T>` resolves to `Vec<T>`.
;
/// Marker for the `Result<T, E>` type constructor, where `E` (the error type) is fixed.
///
/// `ResultHKTMarker<E>` acts as the constructor for `Result<_, E>`.
/// Implements [`HKT`] such that `ResultHKTMarker<E>::Applied<T>` resolves to `Result<T, E>`.
;
/// HKT Marker for `CFn<X, _>`. `X` is the fixed input type of the function.
///
/// Implements [`HKT`] such that `CFnHKTMarker<X>::Applied<Output>` resolves to `CFn<X, Output>`.
// CFnHKTMarker itself doesn't need Debug, Clone etc. unless used directly in ways that require it.
;
/// HKT Marker for `CFnOnce<X, _>`. `X` is the fixed input type of the function.
///
/// Implements [`HKT`] such that `CFnOnceHKTMarker<X>::Applied<Output>` resolves to `CFnOnce<X, Output>`.
;
// --- Arity Markers ---
/// Marks an `HKT` that effectively takes one type argument (e.g., `F<A>`).
///
/// In this library's current HKT simulation, all types implementing [`HKT`]
/// (which defines `type Applied<Arg>`) inherently fit this "arity-1" concept.
/// This trait serves as a blanket implementation for all `T: HKT`, providing
/// a convenient way to specify this arity in trait bounds if needed, although
/// often just `T: HKT` is sufficient.
// Blanket implementation
// If HKTs with more complex arities were needed, e.g., for Bifunctor `F<A, B>`:
// pub trait HKT2 {
// type Applied<Arg1, Arg2>: Sized;
// }
// For now, `HKT` with a single `Applied<Arg>` GAT covers Functor, Applicative, Monad.
// The `concretize` function from the original sketch could be added here if useful.
// It would simply be an identity function on `Self::Applied<Arg>`.
// pub trait HKTConcretize: HKT {
// fn concretize<Arg>(value: Self::Applied<Arg>) -> Self::Applied<Arg> {
// value
// }
// }
// impl<T: HKT> HKTConcretize for T {}