Skip to main content

Freer

Enum Freer 

Source
pub enum Freer<F, A>
where F: HKT + 'static, A: 'static,
{ Pure(A), Impure(Box<dyn FreerStep<F, A>>), }
Expand description

Freer Monad — a free monad that does not require F: Functor.

Freer<F, A> stores a computation as a tree of effect steps, each containing ∃B. (F B, B → Freer F A). The existential B is erased via a dyn-safe trait, deferring the Functor requirement to fold_map.

Pure(a)       — a finished computation
Impure(step)  — an effect step with continuation

§When to use Freer vs Free

  • Use Free<F, A> when F: Functor — simpler, no overhead.
  • Use Freer<F, A> when F is NOT a functor, or when you want to build computations without the functor constraint.

Variants§

§

Pure(A)

A pure value — the computation is finished.

§

Impure(Box<dyn FreerStep<F, A>>)

An effect step with erased intermediate type.

Implementations§

Source§

impl<F, A> Freer<F, A>
where F: HKT + 'static, A: 'static,

Source

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

Wrap a pure value into the freer monad.

Source

pub fn lift_f(fa: <F as HKT>::Of<A>) -> Freer<F, A>
where <F as HKT>::Of<A>: 'static,

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

No F: Functor required.

Source

pub fn fmap<B>(self, f: impl Fn(A) -> B + 'static) -> Freer<F, B>
where B: 'static,

Map a function over the result of this computation.

No F: Functor required. Implemented via chain.

Source

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

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

No F: Functor required. The closure is shared via Rc across deferred chain layers.

Source

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

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

This requires F: Functor (to lower each step) and M: Applicative + Chain (for the target monad operations).

Auto Trait Implementations§

§

impl<F, A> !RefUnwindSafe for Freer<F, A>

§

impl<F, A> !Send for Freer<F, A>

§

impl<F, A> !Sync for Freer<F, A>

§

impl<F, A> !UnwindSafe for Freer<F, A>

§

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

§

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

§

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

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.