pub struct Codensity<F: HKT + 'static, A: 'static> { /* private fields */ }Expand description
Codensity Monad — the CPS transform of a type constructor F.
Codensity<F, A> ≅ ∀R. (A → F R) → F R
This is the right Kan extension of F along itself (Ran F F A),
specialised into a concrete type. The key property: pure, fmap,
and chain require no bounds on F — only to_monad needs
F: Applicative + Chain.
§Use cases
- Monad transformer improvement: wrapping a free monad in Codensity can improve asymptotic performance of left-associated binds.
- CPS conversion: build computations in CPS, then interpret into
the target monad via
to_monad.
Note: Due to Rust’s GAT limitations (type Of<T> cannot add T: 'static),
CodensityF does not implement HKT or Monad. Use inherent methods.
Implementations§
Source§impl<F: HKT + 'static, A: 'static> Codensity<F, A>
impl<F: HKT + 'static, A: 'static> Codensity<F, A>
Sourcepub fn fmap<B: 'static>(self, f: impl Fn(A) -> B + 'static) -> Codensity<F, B>
pub fn fmap<B: 'static>(self, f: impl Fn(A) -> B + 'static) -> Codensity<F, B>
Map a function over the result. No bounds on F required.
Sourcepub fn chain<B: 'static>(
self,
f: impl Fn(A) -> Codensity<F, B> + 'static,
) -> Codensity<F, B>
pub fn chain<B: 'static>( self, f: impl Fn(A) -> Codensity<F, B> + 'static, ) -> Codensity<F, B>
Monadic bind. No bounds on F required.
Sourcepub fn to_monad(self) -> F::Of<A>where
F: Applicative + Chain,
pub fn to_monad(self) -> F::Of<A>where
F: Applicative + Chain,
Collapse the computation into F::Of<A>.
This is the standard way to extract a result from Codensity.
Requires F: Applicative + Chain (i.e., F must be a monad).
Auto Trait Implementations§
impl<F, A> !RefUnwindSafe for Codensity<F, A>
impl<F, A> !Send for Codensity<F, A>
impl<F, A> !Sync for Codensity<F, A>
impl<F, A> !UnwindSafe for Codensity<F, A>
impl<F, A> Freeze for Codensity<F, A>
impl<F, A> Unpin for Codensity<F, A>
impl<F, A> UnsafeUnpin for Codensity<F, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more