pub struct Free<F, A>(/* private fields */)
where
F: Functor + 'static,
A: 'static;Expand description
The Free monad with O(1) bind via CatList.
This implementation follows “Reflection without Remorse” to ensure that left-associated binds do not degrade performance.
§HKT and Lifetime Limitations
Free does not implement HKT traits (like Functor, Monad) from this library.
§The Conflict
- The Traits: The
Kindtrait implemented by theFunctorhierarchy requires the type constructor to accept any lifetime'a(e.g.,type Of<'a, A> = Free<F, A>). - The Implementation: This implementation uses
Box<dyn Any>to type-erase continuations for the “Reflection without Remorse” optimization.dyn Anystrictly requiresA: 'static.
This creates an unresolvable conflict: Free cannot support non-static references (like &'a str),
so it cannot satisfy the Kind signature.
§Why not use the “Naive” Recursive Definition?
A naive definition (enum Free { Pure(A), Roll(F<Box<Free<F, A>>>) }) would support lifetimes
and HKT traits. However, it was rejected because:
- Stack Safety:
runwould not be stack-safe for deep computations. - Performance:
bindwould be O(N), leading to quadratic complexity for sequences of binds.
This implementation prioritizes stack safety and O(1) bind over HKT trait compatibility.
§Type Parameters
F: The base functor (must implementFunctor).A: The result type.
§Examples
use fp_library::{brands::*, types::*};
let free = Free::<ThunkBrand, _>::pure(42);Implementations§
Source§impl<F, A> Free<F, A>where
F: Functor + 'static,
A: 'static,
impl<F, A> Free<F, A>where
F: Functor + 'static,
A: 'static,
Sourcepub fn roll(fa: <F as Kind_cdc7cd43dac7585f>::Of<'static, Free<F, A>>) -> Self
pub fn roll(fa: <F as Kind_cdc7cd43dac7585f>::Of<'static, Free<F, A>>) -> Self
Creates a suspended computation from a functor value.
§Type Signature
forall f a. f (Free f a) -> Free f a
§Parameters
fa: The functor value containing the next step.
§Returns
A Free computation that performs the effect fa.
§Examples
use fp_library::{brands::*, types::*};
let eval = Thunk::new(|| Free::pure(42));
let free = Free::<ThunkBrand, _>::roll(eval);Sourcepub fn bind<B: 'static>(
self,
f: impl FnOnce(A) -> Free<F, B> + 'static,
) -> Free<F, B>
pub fn bind<B: 'static>( self, f: impl FnOnce(A) -> Free<F, B> + 'static, ) -> Free<F, B>
Monadic bind (flatMap) with O(1) complexity.
§Type Signature
forall f b a. (a -> Free f b, Free f a) -> Free f b
§Type Parameters
B: The result type of the new computation.
§Parameters
f: The function to apply to the result of this computation.
§Returns
A new Free computation that chains f after this computation.
§Examples
use fp_library::{brands::*, types::*};
let free = Free::<ThunkBrand, _>::pure(42)
.bind(|x| Free::pure(x + 1));Sourcepub fn run(self) -> Awhere
F: Runnable,
pub fn run(self) -> Awhere
F: Runnable,
Executes the Free computation, returning the final result.
This is the “trampoline” that iteratively processes the CatList of continuations without growing the stack.
§Type Signature
forall f a. Runnable f => Free f a -> a
§Returns
The final result of the computation.
§Examples
use fp_library::{brands::*, types::*};
let free = Free::<ThunkBrand, _>::pure(42);
assert_eq!(free.run(), 42);Trait Implementations§
Auto Trait Implementations§
impl<F, A> Freeze for Free<F, A>
impl<F, A> !RefUnwindSafe for Free<F, A>
impl<F, A> !Send for Free<F, A>
impl<F, A> !Sync for Free<F, A>
impl<F, A> Unpin for Free<F, A>
impl<F, A> !UnwindSafe for Free<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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more