Pure lift lifetime

This commit is contained in:
AF 2023-05-26 09:25:58 +00:00
parent 7f69b10e2e
commit 044ffbcda0
12 changed files with 31 additions and 51 deletions

View File

@ -119,11 +119,9 @@ pub trait Functor: WeakFunctor {
} }
/// Part of [`Applicative`] responsible for Haskell's value lifting, `pure`. /// Part of [`Applicative`] responsible for Haskell's value lifting, `pure`.
pub trait Pure: Functor { pub trait Pure<'a>: 'a + Functor {
/// Equivalent of Haskell's `pure`/`return`. /// Equivalent of Haskell's `pure`/`return`.
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A>;
where
Self: 'a;
} }
/// Part of [`Applicative`] responsible for Haskell's sequential application `<*>`. /// Part of [`Applicative`] responsible for Haskell's sequential application `<*>`.
@ -156,7 +154,7 @@ pub trait ApplicativeTuple<'a>: 'a + Functor {
/// ///
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Applicative.html> /// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Applicative.html>
pub trait Applicative<'a>: pub trait Applicative<'a>:
Pure + ApplicativeSeq<'a> + ApplicativeLA2<'a> + ApplicativeTuple<'a> + ApplicativeSelect<'a> Pure<'a> + ApplicativeSeq<'a> + ApplicativeLA2<'a> + ApplicativeTuple<'a> + ApplicativeSelect<'a>
{ {
/// Equivalent of Haskell's `*>`/`>>`. /// Equivalent of Haskell's `*>`/`>>`.
fn discard_first<A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> { fn discard_first<A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> {
@ -234,7 +232,7 @@ pub trait LocalFunctor: WeakFunctor {
} }
/// Stuff wrapped result into another functor. /// Stuff wrapped result into another functor.
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a; Self: 'a;
} }

View File

@ -22,11 +22,8 @@ impl<C> Functor for ControlFlowInstance<C> {
} }
} }
impl<C> Pure for ControlFlowInstance<C> { impl<'a, C: 'a> Pure<'a> for ControlFlowInstance<C> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
ControlFlow::Break(a) ControlFlow::Break(a)
} }
} }

View File

@ -29,11 +29,8 @@ impl<U: Functor, V: Functor> Functor for CompositionInstance<U, V> {
} }
} }
impl<U: Pure, V: Pure> Pure for CompositionInstance<U, V> { impl<'a, U: Pure<'a>, V: Pure<'a>> Pure<'a> for CompositionInstance<U, V> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
U::pure(V::pure(a)) U::pure(V::pure(a))
} }
} }
@ -155,7 +152,7 @@ impl<U: LocalFunctor + Functor, V: LocalFunctor> LocalFunctor for CompositionIns
U::unstuff(U::fmap(V::unstuff, state)) U::unstuff(U::fmap(V::unstuff, state))
} }
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a, Self: 'a,
{ {

View File

@ -55,11 +55,8 @@ impl<E> Functor for EffectInstance<E> {
} }
} }
impl<E: Effect> Pure for EffectInstance<E> { impl<'a, E: 'a + Effect> Pure<'a> for EffectInstance<E> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
WithEffect { WithEffect {
value: a, value: a,
effect: E::e_pure(), effect: E::e_pure(),
@ -153,7 +150,7 @@ impl<'a, E: 'a + Effect> Monad<'a> for EffectInstance<E> {
} }
impl<E> LocalFunctor for EffectInstance<E> { impl<E> LocalFunctor for EffectInstance<E> {
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a, Self: 'a,
{ {

View File

@ -32,8 +32,8 @@ impl Functor for FutureInstance {
} }
} }
impl Pure for FutureInstance { impl<'a> Pure<'a> for FutureInstance {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
Box::pin(async { a }) Box::pin(async { a })
} }
} }

View File

@ -34,8 +34,8 @@ impl Functor for LazyInstance {
} }
} }
impl Pure for LazyInstance { impl<'a> Pure<'a> for LazyInstance {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
Box::new(|| a) Box::new(|| a)
} }
} }

View File

@ -38,8 +38,8 @@ impl Functor for OptionInstance {
} }
} }
impl Pure for OptionInstance { impl<'a> Pure<'a> for OptionInstance {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
Some(a) Some(a)
} }
} }
@ -119,7 +119,7 @@ impl LocalFunctor for OptionInstance {
} }
} }
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a, Self: 'a,
{ {

View File

@ -41,11 +41,8 @@ impl<T: Functor, O: DeriveFunctor> Functor for OverloadInstance<T, O> {
} }
} }
impl<T: Pure, O: DeriveApplicative> Pure for OverloadInstance<T, O> { impl<'a, T: Pure<'a>, O: 'a + DeriveApplicative> Pure<'a> for OverloadInstance<T, O> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
T::pure(a) T::pure(a)
} }
} }

View File

@ -41,11 +41,8 @@ impl<E> Functor for ResultInstance<E> {
} }
} }
impl<E> Pure for ResultInstance<E> { impl<'a, E: 'a> Pure<'a> for ResultInstance<E> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
Ok(a) Ok(a)
} }
} }
@ -125,7 +122,7 @@ impl<E> LocalFunctor for ResultInstance<E> {
} }
} }
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a, Self: 'a,
{ {

View File

@ -26,8 +26,8 @@ impl Functor for SoloInstance {
} }
} }
impl Pure for SoloInstance { impl<'a> Pure<'a> for SoloInstance {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
a a
} }
} }
@ -103,7 +103,7 @@ impl LocalFunctor for SoloInstance {
state state
} }
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where where
Self: 'a, Self: 'a,
{ {

View File

@ -147,8 +147,8 @@ impl Functor for StacklessInstance {
} }
} }
impl Pure for StacklessInstance { impl<'a> Pure<'a> for StacklessInstance {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
Stackless::from(a) Stackless::from(a)
} }
} }

View File

@ -33,11 +33,8 @@ impl<E> Functor for TryFutureInstance<E> {
} }
} }
impl<E> Pure for TryFutureInstance<E> { impl<'a, E: 'a> Pure<'a> for TryFutureInstance<E> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
where
Self: 'a,
{
Box::pin(async { Ok(a) }) Box::pin(async { Ok(a) })
} }
} }