Skip to main content

Free

Enum Free 

Source
pub enum Free<F, A>
where F: HKT,
{ Pure(A), Roll(Box<<F as HKT>::Of<Free<F, A>>>), }
Expand description

Free Monad — builds a monadic computation as a data structure.

Free<F, A> represents a program where F describes the available effects and A is the result type. Programs are built with pure and lift_f, composed with chain, and interpreted with fold_map using a natural transformation into any target monad.

Pure(a)              — a finished computation returning a
Roll(F<Free<F, A>>)  — one layer of effect wrapping a continuation

Variants§

§

Pure(A)

A pure value — the computation is finished.

§

Roll(Box<<F as HKT>::Of<Free<F, A>>>)

A layer of effect F wrapping a continuation.

Implementations§

Source§

impl<F, A> Free<F, A>
where F: HKT,

Source

pub fn pure(a: A) -> Free<F, A>

Wrap a pure value into the free monad.

Source§

impl<F, A> Free<F, A>
where F: HKT + Functor,

Source

pub fn lift_f(fa: <F as HKT>::Of<A>) -> Free<F, A>

Lift a single effect F<A> into the free monad.

Source

pub fn fmap<B>(self, f: impl Fn(A) -> B) -> Free<F, B>

Map a function over the result of this computation.

Source

pub fn chain<B>(self, f: impl Fn(A) -> Free<F, B>) -> Free<F, B>

Monadic bind — sequence this computation with a function that produces the next computation.

Source

pub fn fold_map<M, NT>(self) -> <M as HKT>::Of<A>
where M: Applicative + Chain, NT: NaturalTransformation<F, M>,

Interpret this free monad into a target monad M using a natural transformation NT: F ~> M.

This is the core interpreter: it collapses the free structure by translating each F effect into M and sequencing with M::chain.

Auto Trait Implementations§

§

impl<F, A> Freeze for Free<F, A>
where A: Freeze,

§

impl<F, A> RefUnwindSafe for Free<F, A>
where A: RefUnwindSafe, <F as HKT>::Of<Free<F, A>>: RefUnwindSafe,

§

impl<F, A> Send for Free<F, A>
where A: Send, <F as HKT>::Of<Free<F, A>>: Send,

§

impl<F, A> Sync for Free<F, A>
where A: Sync, <F as HKT>::Of<Free<F, A>>: Sync,

§

impl<F, A> Unpin for Free<F, A>
where A: Unpin,

§

impl<F, A> UnsafeUnpin for Free<F, A>
where A: UnsafeUnpin,

§

impl<F, A> UnwindSafe for Free<F, A>
where A: UnwindSafe, <F as HKT>::Of<Free<F, A>>: UnwindSafe,

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.