Skip to main content

Codensity

Struct Codensity 

Source
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>

Source

pub fn pure(a: A) -> Self

Wrap a pure value. No bounds on F required.

Source

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.

Source

pub fn chain<B: 'static>( self, f: impl Fn(A) -> Codensity<F, B> + 'static, ) -> Codensity<F, B>

Monadic bind. No bounds on F required.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.