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
//! Monad Transformers for composing monadic effects.
//!
//! This module provides monad transformer implementations, which allow composing different
//! monadic effects into a single monad. Monad transformers solve the problem of using multiple
//! monads together without excessive nesting.
//!
//! # What are Monad Transformers?
//!
//! Monad transformers allow you to:
//!
//! - Combine multiple monadic effects (like option, state, reader, etc.)
//! - Access the operations of all combined monads through a unified interface
//! - Avoid deeply nested monadic types
//!
//! # Core Concepts
//!
//! The key components of the monad transformer pattern:
//!
//! - **Base Monad**: The innermost monad being transformed (e.g., `Result`, `Option`, `Vec`)
//! - **Transformer**: A wrapper that adds new effects while preserving the interface
//! - **Lift**: Operations to promote values from the base monad to the transformer
//! - **Stack**: The combination of transformers and base monad creates a "stack" of effects
//!
//! # Transformer Stacks
//!
//! Transformers are typically used in stacks, with each transformer adding a new capability:
//!
//! ```text
//! ReaderT<StateT<Option<_>>> = Environment + State + Optionality
//! ```
//!
//! In this example:
//! - `Option<_>` is the base monad, providing optional computation
//! - `StateT<_>` transforms it to add state management
//! - `ReaderT<_>` adds environment access on top
//!
//! # Available Transformers
//!
//! This module provides the following monad transformers:
//!
//! - [`OptionT`]: Adds optionality to any base monad
//! - [`EitherT`]: Adds error handling with a specific error type
//! - [`ReaderT`]: Adds environment/configuration reading capabilities
//! - [`StateT`]: Adds stateful computation capabilities
//!
//! It also exports two monads that carry transformer-style names but do not
//! (yet) take a base monad:
//!
//! - [`Scriptor`]: a plain Writer monad (value + accumulated log)
//! - [`ContinuatioT`]: a plain continuation monad (CPS)
//!
//! # CPS Transformers (Phase 6)
//!
//! For O(1) bind composition, use the CPS (Church-encoded) variant
//! (requires the `transformers-cps` feature):
//! - [`ecclesia::LectorEcclesiaT`]: CPS `ReaderT`
//!
//! # Example Usage
//!
//! ```
//! # #[cfg(feature = "alloc")]
//! # fn main() {
//! use ordofp_core::transformers::{OptionT, MonadTransformer};
//!
//! // OptionT over Result - combines optionality with error handling
//! let opt_result: OptionT<Result<Option<i32>, &str>> = OptionT::some(42);
//! let mapped = opt_result.map(|x| x * 2);
//! assert_eq!(mapped.run(), Ok(Some(84)));
//!
//! // Chaining computations
//! let chained = OptionT::<Result<Option<i32>, &str>>::some(10)
//! .flat_map(|x| {
//! if x > 5 { OptionT::some(x * 2) }
//! else { OptionT::none() }
//! });
//! assert_eq!(chained.run(), Ok(Some(20)));
//! # }
//! # #[cfg(not(feature = "alloc"))]
//! # fn main() {}
//! ```
//!
//! # Implementation Pattern
//!
//! Monad transformers generally follow this implementation pattern:
//!
//! 1. Define a new type that wraps a function or value with the base monad inside
//! 2. Implement the [`MonadTransformer`] trait to provide lifting capabilities
//! 3. Implement `Functor`, `Apply`, `Applicative`, and `Monad` operations
//! 4. Provide additional methods specific to the transformer (like `run`, `exec`, etc.)
extern crate alloc;
// Async transformers (OrdoFP 2.0)
// Requires the "async" feature flag
pub use either_tEitherT;
pub use option_tOptionT;
pub use reader_tReaderT;
pub use state_tStateT;
pub use writer_t::;
pub use cont_tContinuatioT;
/// Trait for monad transformers.
///
/// This trait provides a common interface for all monad transformers, allowing
/// them to be used in a Universalis way regardless of the specific transformer type.
/// By implementing this trait, a type declares its capability to lift values
/// from a base monad into the transformer context.
///
/// # Laws
///
/// Implementations must satisfy these laws:
///
/// 1. **Lift Preserves Identity:**
/// ```text
/// lift(pure(x)) == pure(x)
/// ```
///
/// 2. **Lift Preserves Bind:**
/// ```text
/// lift(m).flat_map(|x| lift(f(x))) == lift(m.flat_map(f))
/// ```
///
/// # Example
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # fn main() {
/// use ordofp_core::transformers::{OptionT, MonadTransformer};
///
/// // Lift a Result into OptionT
/// let base: Result<i32, &str> = Ok(42);
/// let lifted: OptionT<Result<Option<i32>, &str>> = OptionT::lift_m(base);
/// assert_eq!(lifted.run(), Ok(Some(42)));
/// # }
/// # #[cfg(not(feature = "alloc"))]
/// # fn main() {}
/// ```
/// Helper function to lift a value from a base monad into a monad transformer.
///
/// This function provides a convenient way to lift values without needing to
/// specify the transformer type explicitly in many cases.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # fn main() {
/// use ordofp_core::transformers::{OptionT, lift};
///
/// let base: Result<i32, &str> = Ok(42);
/// let lifted: OptionT<Result<Option<i32>, &str>> = lift(base);
/// assert_eq!(lifted.run(), Ok(Some(42)));
/// # }
/// # #[cfg(not(feature = "alloc"))]
/// # fn main() {}
/// ```