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
//! # Monad Transformers
//!
//! 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:
//!
//! - `ReaderT`: Adds environment capabilities
//! - Makes an environment value available throughout a computation
//! - Useful for dependency injection and configuration
//!
//! - `StateT`: Adds stateful computation capabilities
//! - Allows passing and modifying state through a computation
//! - Useful for tracking changing values without mutation
//!
//! - `ContT`: Adds continuation-passing style capabilities
//! - Enables advanced control flow patterns like early exit and backtracking
//! - Useful for implementing coroutines and complex control structures
//!
//! ## 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 the `Monad` trait to allow composition with other monads
//! 4. Provide additional methods specific to the transformer (like `run`, `exec`, etc.)
//!
//! ## Stacking Transformers
//!
//! When stacking multiple transformers, you'll need to lift values through each layer:
//!
//! ```text
//! // If you have a base monad value:
//! let base_value: Option<T> = Some(value);
//!
//! // Lift through multiple layers:
//! let value_in_state_reader_t = lift::<ReaderT<_, _, _>>(lift::<StateT<_, _, _>>(base_value));
//! ```
//!
//! ## Performance Considerations
//!
//! Monad transformers add some overhead due to their layered nature. Consider:
//!
//! - Each transformer adds a level of indirection
//! - Deep stacks may impact performance in critical sections
//! - More complex type signatures with multiple transformers
//!
//! For performance-critical code, consider using specialized combined monads
//! instead of deep transformer stacks.
use crateMonad;
pub use cont_tContT;
pub use reader_tReaderT;
pub use state_tStateT;
/// Trait for monad transformers.
///
/// This trait provides a common interface for all monad transformers, allowing
/// them to be used in a generic 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.
///
/// # Type Parameters
///
/// The trait requires a `BaseMonad` associated type that specifies which monad
/// is being transformed. This type must itself implement the `Monad` trait.
///
/// # Laws
///
/// Implementations must satisfy these laws:
///
/// 1. **Lift Preserves Identity:**
/// For any base monad `m` and transformer `T`:
/// ```text
/// T::lift(m.pure(x)) == T::pure(x)
/// ```
///
/// 2. **Lift Preserves Bind:**
/// For any base monad `m` and transformer `T`:
/// ```text
/// T::lift(m.bind(f)) == T::lift(m).bind(|x| T::lift(f(x)))
/// ```
///
/// # Examples
///
/// Implementing `MonadTransformer` for a state transformer:
///
/// ```rust
/// # use rustica::traits::monad::Monad;
/// # use rustica::transformers::MonadTransformer;
/// # use std::marker::PhantomData;
/// # struct StateT<S, M, A> {
/// # run: Box<dyn Fn(&S) -> M>,
/// # _state: PhantomData<S>,
/// # _output: PhantomData<A>,
/// # }
/// # impl<S, M: Monad, A> StateT<S, M, A> {
/// # fn new<F>(f: F) -> Self
/// # where
/// # F: Fn(&S) -> M + 'static,
/// # {
/// # StateT {
/// # run: Box::new(f),
/// # _state: PhantomData,
/// # _output: PhantomData,
/// # }
/// # }
/// # }
///
/// impl<S, M, A> MonadTransformer for StateT<S, M, A>
/// where
/// M: Monad + Clone + 'static,
/// S: 'static,
/// A: 'static,
/// {
/// type BaseMonad = M;
///
/// fn lift(base: Self::BaseMonad) -> Self {
/// StateT::new(move |_state| base.clone())
/// }
/// }
/// ```
/// Lifts a value from a base monad into a monad transformer.
///
/// This function provides a convenient way to lift values from a base monad
/// into a monad transformer without having to specify the transformer type
/// explicitly in the code. It uses Rust's type inference to determine the
/// correct transformer.
///
/// # Type Parameters
///
/// * `T` - The monad transformer type
/// * `M` - The base monad type
///
/// # Parameters
///
/// * `m` - A value in the base monad
///
/// # Returns
///
/// A value in the transformed monad
///
/// # Examples
///
/// ```rust
/// # use rustica::transformers::{reader_t::ReaderT, lift};
/// # use rustica::traits::monad::Monad;
/// # #[derive(Clone)]
/// # struct Env;
///
/// // Create a base monad value
/// let option_value: Option<i32> = Some(42);
///
/// // Lift it into a ReaderT transformer
/// let reader_t_value = lift::<ReaderT<Env, Option<i32>, i32>, Option<i32>>(option_value);
///
/// // Use the lifted value
/// let result = reader_t_value.run_reader(Env);
/// assert_eq!(result, Some(42));
/// ```