From 2c51b7a2e4dca6ba6eeb124cd128b48cf7041b96 Mon Sep 17 00:00:00 2001 From: d-plaindoux Date: Fri, 15 Sep 2023 14:12:56 +0200 Subject: [PATCH] Deep code review introducing lifetime required for Reader, Writer etc. --- src/core/hkp.rs | 4 +-- src/core/transform.rs | 10 +++--- src/specs/applicative.rs | 24 ++++++++------- src/specs/bind.rs | 17 ++++++----- src/specs/functor.rs | 18 +++++------ src/specs/monad.rs | 12 ++++---- src/standard/either.rs | 52 +++++++++++++++---------------- src/standard/identity.rs | 52 +++++++++++++++---------------- src/standard/mod.rs | 4 +++ src/standard/option.rs | 50 +++++++++++++++--------------- src/standard/reader.rs | 22 ++++++++++++++ src/standard/result.rs | 52 +++++++++++++++---------------- src/standard/vec.rs | 64 +++++++++++++++++++-------------------- tests/test_applicative.rs | 10 +++--- tests/test_bind.rs | 18 +++++------ tests/test_functor.rs | 5 +-- tests/test_monad.rs | 7 +++-- 17 files changed, 228 insertions(+), 193 deletions(-) create mode 100644 src/standard/reader.rs diff --git a/src/core/hkp.rs b/src/core/hkp.rs index b52dbb2..b4ccb5f 100644 --- a/src/core/hkp.rs +++ b/src/core/hkp.rs @@ -1,3 +1,3 @@ -pub trait HKP { - type T; +pub trait HKP<'a> { + type T; } diff --git a/src/core/transform.rs b/src/core/transform.rs index cbec29d..24f80bf 100644 --- a/src/core/transform.rs +++ b/src/core/transform.rs @@ -1,11 +1,11 @@ use crate::core::hkp::HKP; -pub trait Transform: HKP { - type This: HKP; +pub trait Transform<'a, A>: HKP<'a> { + type This: HKP<'a>; - fn from_hkp(a: ::T) -> Self::T; + fn from_hkp(a: >::T) -> Self::T; - fn from_self(a: Self::T) -> ::T; + fn from_self(a: Self::T) -> >::T; - fn to_hkp(self) -> ::T; + fn to_hkp(self) -> >::T; } diff --git a/src/specs/applicative.rs b/src/specs/applicative.rs index 064abe1..0a78076 100644 --- a/src/specs/applicative.rs +++ b/src/specs/applicative.rs @@ -1,31 +1,33 @@ use crate::specs::functor::Functor; -pub trait Applicative: Functor { +pub trait Applicative<'a>: Functor<'a> { fn pure(a: A) -> Self::T; fn apply(mf: Self::T, ma: Self::T) -> Self::T - where - A: Clone, - MAP: Fn(A) -> B; + where + A: Clone, + MAP: Fn(A) -> B; } pub mod infix { use crate::core::transform::Transform; use crate::specs::applicative::Applicative as Api; - pub trait Applicative: Transform=Self::TL, This=Self::ThisL> { - type ThisL: Api; - type TL: Applicative; + pub trait Applicative<'a, A: 'a>: + Transform<'a, A, T = Self::TL, This = Self::ThisL> + { + type ThisL: Api<'a>; + type TL: Applicative<'a, B>; fn pure(a: B) -> Self::T { Self::from_hkp(Self::This::pure(a)) } fn apply(self, mf: Self::T) -> Self::T - where - A: Clone, - MAP: Fn(A) -> B, - Self: Sized, + where + A: Clone, + MAP: Fn(A) -> B, + Self: Sized, { Self::from_hkp(Self::This::apply(Self::from_self::(mf), self.to_hkp())) } diff --git a/src/specs/bind.rs b/src/specs/bind.rs index fa45dc0..c49231f 100644 --- a/src/specs/bind.rs +++ b/src/specs/bind.rs @@ -1,11 +1,14 @@ use crate::specs::applicative::Applicative; -pub trait Bind: Applicative { +pub trait Bind<'a>: Applicative<'a> +where + Self: 'a, +{ fn join(mma: Self::T>) -> Self::T; fn bind(ma: Self::T, mf: BIND) -> Self::T where - BIND: Fn(A) -> Self::T, + BIND: Fn(A) -> Self::T + 'a, { Self::join(Self::map(mf, ma)) } @@ -15,9 +18,9 @@ pub mod infix { use crate::core::transform::Transform; use crate::specs::bind::Bind as Api; - pub trait Bind: Transform = Self::TL, This = Self::ThisL> { - type ThisL: Api; - type TL: Bind; + pub trait Bind<'a, A: 'a>: Transform<'a, A, T = Self::TL, This = Self::ThisL> { + type ThisL: Api<'a>; + type TL: Bind<'a, B>; fn join(mma: Self::T>) -> Self::T { Self::from_hkp(Self::This::bind(Self::from_self(mma), |a| { @@ -27,10 +30,10 @@ pub mod infix { fn bind(self, mf: BIND) -> Self::T where - BIND: Fn(A) -> Self::T, + BIND: Fn(A) -> Self::T + 'a, Self: Sized, { - Self::from_hkp(Self::This::bind(self.to_hkp(), |a| { + Self::from_hkp(Self::This::bind(self.to_hkp(), move |a| { Self::from_self::(mf(a)) })) } diff --git a/src/specs/functor.rs b/src/specs/functor.rs index 22db598..5650b7e 100644 --- a/src/specs/functor.rs +++ b/src/specs/functor.rs @@ -1,23 +1,23 @@ use crate::core::hkp::HKP; -pub trait Functor: HKP { +pub trait Functor<'a>: HKP<'a> { fn map(f: MAP, ma: Self::T) -> Self::T - where - MAP: Fn(A) -> B; + where + MAP: Fn(A) -> B + 'a; } pub mod infix { use crate::core::transform::Transform; use crate::specs::functor::Functor as Api; - pub trait Functor: Transform=Self::TL, This=Self::ThisL> { - type ThisL: Api; - type TL: Functor; + pub trait Functor<'a, A: 'a>: Transform<'a, A, T = Self::TL, This = Self::ThisL> { + type ThisL: Api<'a>; + type TL: Functor<'a, B>; fn map(self, f: MAP) -> Self::T - where - MAP: Fn(A) -> B, - Self: Sized, + where + MAP: Fn(A) -> B + 'a, + Self: Sized, { Self::from_hkp(Self::This::map(f, self.to_hkp())) } diff --git a/src/specs/monad.rs b/src/specs/monad.rs index b3a4ff1..9a74682 100644 --- a/src/specs/monad.rs +++ b/src/specs/monad.rs @@ -1,6 +1,6 @@ use crate::specs::bind::Bind; -pub trait Monad: Bind { +pub trait Monad<'a>: Bind<'a> { fn returns(a: A) -> Self::T { Self::pure(a) } @@ -11,10 +11,10 @@ pub mod infix { use crate::specs::bind::Bind; use crate::specs::monad::Monad as Api; - pub trait Monad: Transform = Self::TL, This = Self::ThisL> { - type ThisL: Api; + pub trait Monad<'a, A: 'a>: Transform<'a, A, T = Self::TL, This = Self::ThisL> { + type ThisL: Api<'a>; - type TL: Monad; + type TL: Monad<'a, B>; fn returns(b: B) -> Self::T { Self::from_hkp(Self::This::returns(b)) @@ -28,10 +28,10 @@ pub mod infix { fn bind(self, mf: BIND) -> Self::T where - BIND: Fn(A) -> Self::T, + BIND: Fn(A) -> Self::T + 'a, Self: Sized, { - Self::from_hkp(::bind(self.to_hkp(), |a| { + Self::from_hkp(::bind(self.to_hkp(), move |a| { Self::from_self::(mf(a)) })) } diff --git a/src/standard/either.rs b/src/standard/either.rs index 61d960e..f47f914 100644 --- a/src/standard/either.rs +++ b/src/standard/either.rs @@ -16,14 +16,14 @@ pub enum Either { Right(R), } -impl HKP for EitherK { - type T = Either; +impl<'a, L> HKP<'a> for EitherK { + type T = Either; } -impl Functor for EitherK { - fn map(f: MAP, ma: Self::T) -> Self::T +impl<'a, E> Functor<'a> for EitherK { + fn map(f: MAP, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { match ma { Left(l) => Left(l), @@ -32,14 +32,14 @@ impl Functor for EitherK { } } -impl Applicative for EitherK { - fn pure(a: A) -> Self::T { +impl<'a, E> Applicative<'a> for EitherK { + fn pure(a: A) -> Self::T { Right(a) } - fn apply(mf: Self::T, ma: Self::T) -> Self::T + fn apply(mf: Self::T, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { match mf { Left(l) => Left(l), @@ -48,8 +48,8 @@ impl Applicative for EitherK { } } -impl Bind for EitherK { - fn join(mma: Self::T>) -> Self::T { +impl<'a, E: 'a> Bind<'a> for EitherK { + fn join(mma: Self::T>) -> Self::T { match mma { Left(ma) => Left(ma), Right(r) => r, @@ -57,7 +57,7 @@ impl Bind for EitherK { } } -impl Monad for EitherK {} +impl<'a, E: 'a> Monad<'a> for EitherK {} mod infix { use crate::core::hkp::HKP; @@ -68,43 +68,43 @@ mod infix { use crate::specs::monad::infix::Monad; use crate::standard::either::{Either, EitherK}; - impl HKP for Either { - type T = Either; + impl<'a, L, R> HKP<'a> for Either { + type T = Either; } - impl Transform for Either { + impl<'a, L, R: 'a> Transform<'a, R> for Either { type This = EitherK; - fn from_hkp(a: ::T) -> Self::T { + fn from_hkp(a: >::T) -> Self::T { a } - fn from_self(a: Self::T) -> ::T { + fn from_self(a: Self::T) -> >::T { a } - fn to_hkp(self) -> ::T { + fn to_hkp(self) -> >::T { self } } - impl Functor for Either { + impl<'a, L, R: 'a> Functor<'a, R> for Either { type ThisL = EitherK; - type TL = Either; + type TL = Either; } - impl Applicative for Either { + impl<'a, L, R: 'a> Applicative<'a, R> for Either { type ThisL = EitherK; - type TL = Either; + type TL = Either; } - impl Bind for Either { + impl<'a, L: 'a, R: 'a> Bind<'a, R> for Either { type ThisL = EitherK; - type TL = Either; + type TL = Either; } - impl Monad for Either { + impl<'a, L: 'a, R: 'a> Monad<'a, R> for Either { type ThisL = EitherK; - type TL = Either; + type TL = Either; } } diff --git a/src/standard/identity.rs b/src/standard/identity.rs index 8e2ffbd..0431460 100644 --- a/src/standard/identity.rs +++ b/src/standard/identity.rs @@ -10,39 +10,39 @@ pub struct Identity { value: A, } -impl HKP for IdentityK { - type T = Identity; +impl<'a> HKP<'a> for IdentityK { + type T = Identity; } -impl Functor for IdentityK { - fn map(f: MAP, ma: Self::T) -> Self::T +impl<'a> Functor<'a> for IdentityK { + fn map(f: MAP, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { Identity { value: f(ma.value) } } } -impl Applicative for IdentityK { - fn pure(a: A) -> Self::T { +impl<'a> Applicative<'a> for IdentityK { + fn pure(a: A) -> Self::T { Identity { value: a } } - fn apply(mf: Self::T, ma: Self::T) -> Self::T + fn apply(mf: Self::T, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { Self::map(mf.value, ma) } } -impl Bind for IdentityK { - fn join(mma: Self::T>) -> Self::T { +impl<'a> Bind<'a> for IdentityK { + fn join(mma: Self::T>) -> Self::T { mma.value } } -impl Monad for IdentityK {} +impl Monad<'_> for IdentityK {} pub mod infix { use crate::core::hkp::HKP; @@ -53,43 +53,43 @@ pub mod infix { use crate::specs::monad::infix::Monad; use crate::standard::identity::{Identity, IdentityK}; - impl HKP for Identity { - type T = Identity; + impl<'a, A> HKP<'a> for Identity { + type T = Identity; } - impl Transform for Identity { + impl<'a, A: 'a> Transform<'a, A> for Identity { type This = IdentityK; - fn from_hkp(a: ::T) -> Self::T { + fn from_hkp(a: >::T) -> Self::T { a } - fn from_self(a: Self::T) -> ::T { + fn from_self(a: Self::T) -> >::T { a } - fn to_hkp(self) -> ::T { + fn to_hkp(self) -> >::T { self } } - impl Functor for Identity { + impl<'a, A: 'a> Functor<'a, A> for Identity { type ThisL = IdentityK; - type TL = Identity; + type TL = Identity; } - impl Applicative for Identity { + impl<'a, A: 'a> Applicative<'a, A> for Identity { type ThisL = IdentityK; - type TL = Identity; + type TL = Identity; } - impl Bind for Identity { + impl<'a, A: 'a> Bind<'a, A> for Identity { type ThisL = IdentityK; - type TL = Identity; + type TL = Identity; } - impl Monad for Identity { + impl<'a, A: 'a> Monad<'a, A> for Identity { type ThisL = IdentityK; - type TL = Identity; + type TL = Identity; } } diff --git a/src/standard/mod.rs b/src/standard/mod.rs index 829aee0..0b9cdbc 100644 --- a/src/standard/mod.rs +++ b/src/standard/mod.rs @@ -1,5 +1,9 @@ +/* + +*/ pub mod either; pub mod identity; pub mod option; +pub mod reader; pub mod result; pub mod vec; diff --git a/src/standard/option.rs b/src/standard/option.rs index 958318c..0554dc1 100644 --- a/src/standard/option.rs +++ b/src/standard/option.rs @@ -6,12 +6,12 @@ use crate::specs::monad::Monad; pub struct OptionK; -impl HKP for OptionK { - type T = Option; +impl<'a> HKP<'a> for OptionK { + type T = Option; } -impl Functor for OptionK { - fn map(f: MAP, ma: Self::T) -> Self::T +impl<'a> Functor<'a> for OptionK { + fn map(f: MAP, ma: Self::T) -> Self::T where MAP: Fn(A) -> B, { @@ -19,14 +19,14 @@ impl Functor for OptionK { } } -impl Applicative for OptionK { - fn pure(a: A) -> Self::T { +impl<'a> Applicative<'a> for OptionK { + fn pure(a: A) -> Self::T { Some(a) } - fn apply(mf: Self::T, ma: Self::T) -> Self::T + fn apply(mf: Self::T, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { match mf { Some(f) => Self::map(f, ma), @@ -35,8 +35,8 @@ impl Applicative for OptionK { } } -impl Bind for OptionK { - fn join(mma: Self::T>) -> Self::T { +impl<'a> Bind<'a> for OptionK { + fn join(mma: Self::T>) -> Self::T { match mma { Some(ma) => ma, None => None, @@ -44,7 +44,7 @@ impl Bind for OptionK { } } -impl Monad for OptionK {} +impl Monad<'_> for OptionK {} pub mod infix { use crate::core::hkp::HKP; @@ -55,43 +55,43 @@ pub mod infix { use crate::specs::monad::infix::Monad; use crate::standard::option::OptionK; - impl HKP for Option { - type T = Option; + impl<'a, A> HKP<'a> for Option { + type T = Option; } - impl Transform for Option { + impl<'a, A: 'a> Transform<'a, A> for Option { type This = OptionK; - fn from_hkp(a: ::T) -> Self::T { + fn from_hkp(a: >::T) -> Self::T { a } - fn from_self(a: Self::T) -> ::T { + fn from_self(a: Self::T) -> >::T { a } - fn to_hkp(self) -> ::T { + fn to_hkp(self) -> >::T { self } } - impl Functor for Option { + impl<'a, A: 'a> Functor<'a, A> for Option { type ThisL = OptionK; - type TL = Option; + type TL = Option; } - impl Applicative for Option { + impl<'a, A: 'a> Applicative<'a, A> for Option { type ThisL = OptionK; - type TL = Option; + type TL = Option; } - impl Bind for Option { + impl<'a, A: 'a> Bind<'a, A> for Option { type ThisL = OptionK; - type TL = Option; + type TL = Option; } - impl Monad for Option { + impl<'a, A: 'a> Monad<'a, A> for Option { type ThisL = OptionK; - type TL = Option; + type TL = Option; } } diff --git a/src/standard/reader.rs b/src/standard/reader.rs new file mode 100644 index 0000000..6a46b72 --- /dev/null +++ b/src/standard/reader.rs @@ -0,0 +1,22 @@ +use crate::core::hkp::HKP; +use crate::specs::functor::Functor; +use std::marker::PhantomData; + +pub struct Reader<'a, E, F: HKP<'a>, A: 'a>(F, Box F::T + 'a>); + +pub struct ReaderK<'a, E, F: HKP<'a>>(PhantomData<&'a E>, PhantomData); + +impl<'a, E, F: HKP<'a>> HKP<'a> for ReaderK<'a, E, F> { + type T = Reader<'a, E, F, B>; +} + +impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> { + fn map(f: MAP, ma: Self::T) -> Self::T + where + MAP: Fn(A) -> B + 'e, + { + let Reader(functor, va) = ma; + let run = |e| F::map::(f, va(e)); + Reader(functor, Box::new(run)) + } +} diff --git a/src/standard/result.rs b/src/standard/result.rs index bf1c135..6a4924f 100644 --- a/src/standard/result.rs +++ b/src/standard/result.rs @@ -10,27 +10,27 @@ pub struct ResultK { _ignore: PhantomData, } -impl HKP for ResultK { - type T = Result; +impl<'a, E> HKP<'a> for ResultK { + type T = Result; } -impl Functor for ResultK { - fn map(f: MAP, ma: Self::T) -> Self::T +impl<'a, E> Functor<'a> for ResultK { + fn map(f: MAP, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { ma.map(f) } } -impl Applicative for ResultK { - fn pure(a: A) -> Self::T { +impl<'a, E> Applicative<'a> for ResultK { + fn pure(a: A) -> Self::T { Ok(a) } - fn apply(mf: Self::T, ma: Self::T) -> Self::T + fn apply(mf: Self::T, ma: Self::T) -> Self::T where - MAP: Fn(A) -> B, + MAP: Fn(A) -> B + 'a, { match mf { Ok(f) => Self::map(f, ma), @@ -39,8 +39,8 @@ impl Applicative for ResultK { } } -impl Bind for ResultK { - fn join(mma: Self::T>) -> Self::T { +impl<'a, E: 'a> Bind<'a> for ResultK { + fn join(mma: Self::T>) -> Self::T { match mma { Ok(ma) => ma, Err(e) => Err(e), @@ -48,7 +48,7 @@ impl Bind for ResultK { } } -impl Monad for ResultK {} +impl<'a, E: 'a> Monad<'a> for ResultK {} mod infix { use crate::core::hkp::HKP; @@ -59,43 +59,43 @@ mod infix { use crate::specs::monad::infix::Monad; use crate::standard::result::ResultK; - impl HKP for Result { - type T = Result; + impl<'a, A: 'a, E> HKP<'a> for Result { + type T = Result; } - impl Transform for Result { + impl<'a, A: 'a, E> Transform<'a, A> for Result { type This = ResultK; - fn from_hkp(a: ::T) -> Self::T { + fn from_hkp(a: >::T) -> Self::T { a } - fn from_self(a: Self::T) -> ::T { + fn from_self(a: Self::T) -> >::T { a } - fn to_hkp(self) -> ::T { + fn to_hkp(self) -> >::T { self } } - impl Functor for Result { + impl<'a, A: 'a, E> Functor<'a, A> for Result { type ThisL = ResultK; - type TL = Result; + type TL = Result; } - impl Applicative for Result { + impl<'a, A: 'a, E> Applicative<'a, A> for Result { type ThisL = ResultK; - type TL = Result; + type TL = Result; } - impl Bind for Result { + impl<'a, A: 'a, E: 'a> Bind<'a, A> for Result { type ThisL = ResultK; - type TL = Result; + type TL = Result; } - impl Monad for Result { + impl<'a, A: 'a, E: 'a> Monad<'a, A> for Result { type ThisL = ResultK; - type TL = Result; + type TL = Result; } } diff --git a/src/standard/vec.rs b/src/standard/vec.rs index d66391e..55ecb67 100644 --- a/src/standard/vec.rs +++ b/src/standard/vec.rs @@ -6,48 +6,48 @@ use crate::specs::monad::Monad; pub struct VecK; -impl HKP for VecK { - type T = Vec; +impl<'a> HKP<'a> for VecK { + type T = Vec; } -impl Functor for VecK { - fn map(f: MAP, ma: Self::T) -> Self::T - where - MAP: Fn(A) -> B, +impl<'a> Functor<'a> for VecK { + fn map(f: MAP, ma: Self::T) -> Self::T + where + MAP: Fn(A) -> B + 'a, { ma.into_iter().map(f).collect() } } -impl Applicative for VecK { - fn pure(a: A) -> Self::T { +impl<'a> Applicative<'a> for VecK { + fn pure(a: A) -> Self::T { vec![a] } - fn apply(mf: Self::T, ma: Self::T) -> Self::T - where - A: Clone, - MAP: Fn(A) -> B, + fn apply(mf: Self::T, ma: Self::T) -> Self::T + where + A: Clone, + MAP: Fn(A) -> B + 'a, { let new_ma = || ma.to_vec().into_iter(); VecK::join(mf.into_iter().map(|f| new_ma().map(f).collect()).collect()) } } -impl Bind for VecK { - fn join(mma: Self::T>) -> Self::T { +impl<'a> Bind<'a> for VecK { + fn join(mma: Self::T>) -> Self::T { mma.into_iter().flatten().collect() } - fn bind(ma: Self::T, mf: BIND) -> Self::T - where - BIND: Fn(A) -> Self::T, + fn bind(ma: Self::T, mf: BIND) -> Self::T + where + BIND: Fn(A) -> Self::T + 'a, { ma.into_iter().flat_map(mf).collect() } } -impl Monad for VecK {} +impl<'a> Monad<'a> for VecK {} pub mod infix { use crate::core::hkp::HKP; @@ -58,43 +58,43 @@ pub mod infix { use crate::specs::monad::infix::Monad; use crate::standard::vec::VecK; - impl HKP for Vec { - type T = Vec; + impl<'a, A> HKP<'a> for Vec { + type T = Vec; } - impl Transform for Vec { + impl<'a, A: 'a> Transform<'a, A> for Vec { type This = VecK; - fn from_hkp(a: ::T) -> Self::T { + fn from_hkp(a: >::T) -> Self::T { a } - fn from_self(a: Self::T) -> ::T { + fn from_self(a: Self::T) -> >::T { a } - fn to_hkp(self) -> ::T { + fn to_hkp(self) -> >::T { self } } - impl Functor for Vec { + impl<'a, A: 'a> Functor<'a, A> for Vec { type ThisL = VecK; - type TL = Vec; + type TL = Vec; } - impl Applicative for Vec { + impl<'a, A: 'a> Applicative<'a, A> for Vec { type ThisL = VecK; - type TL = Vec; + type TL = Vec; } - impl Bind for Vec { + impl<'a, A: 'a> Bind<'a, A> for Vec { type ThisL = VecK; - type TL = Vec; + type TL = Vec; } - impl Monad for Vec { + impl<'a, A: 'a> Monad<'a, A> for Vec { type ThisL = VecK; - type TL = Vec; + type TL = Vec; } } diff --git a/tests/test_applicative.rs b/tests/test_applicative.rs index 5df224a..2f247bf 100644 --- a/tests/test_applicative.rs +++ b/tests/test_applicative.rs @@ -5,11 +5,11 @@ mod tests_apply { use maitar::standard::result::ResultK; use maitar::standard::vec::VecK; - fn test_apply(ma: This::T) -> This::T { + fn test_apply<'a, This: Applicative<'a>>(ma: This::T) -> This::T { This::apply(This::pure(|i| i + 1), ma) } - fn test_apply_with_f( + fn test_apply_with_f<'a, This: Applicative<'a>>( f: This::T i32>, ma: This::T, ) -> This::T { @@ -64,7 +64,7 @@ mod tests_apply { type Int2Int = fn(i32) -> i32; - fn test_apply_with_f=This>>( + fn test_apply_with_f<'a, This: Applicative<'a, i32, TL = This>>( f: This::T, g: This::T, ma: This, @@ -72,7 +72,9 @@ mod tests_apply { ma.apply::<_, Int2Int>(f).apply::<_, Int2Int>(g) } - fn test_apply=Infix>>(ma: Infix) -> Infix::T { + fn test_apply<'a, Infix: Applicative<'a, i32, TL = Infix>>( + ma: Infix, + ) -> Infix::T { test_apply_with_f( Infix::pure::(|i| i - 1), Infix::pure::(|i| i + 2), diff --git a/tests/test_bind.rs b/tests/test_bind.rs index becc3b0..fc7617e 100644 --- a/tests/test_bind.rs +++ b/tests/test_bind.rs @@ -1,10 +1,10 @@ #[cfg(test)] mod tests_join { + use maitar::specs::bind::Bind; use maitar::standard::option::OptionK; use maitar::standard::result::ResultK; - - fn test_join(mma: This::T>) -> This::T { + fn test_join<'a, This: Bind<'a>>(mma: This::T>) -> This::T { This::join(mma) } @@ -52,7 +52,7 @@ mod tests_bind { use maitar::standard::result::ResultK; use maitar::standard::vec::VecK; - fn test_bind(ma: This::T, f: fn(i32) -> This::T) -> This::T { + fn test_bind<'a, This: Bind<'a>>(ma: This::T, f: fn(i32) -> This::T) -> This::T { This::bind(ma, f) } @@ -90,15 +90,15 @@ mod tests_bind { use maitar::core::hkp::HKP; use maitar::specs::bind::infix::Bind; - type Int2Int = fn(i32) -> ::T; + type Int2Int<'a, This> = fn(i32) -> >::T; - fn test_bind_with_f = This>>( - f: Int2Int, - g: Int2Int, + fn test_bind_with_f<'a, This: Bind<'a, i32, TL = This> + 'a>( + f: Int2Int<'a, This>, + g: Int2Int<'a, This>, ma: This, ) -> This::T { - ma.bind::>(f) - .bind::>(g) + ma.bind::>(f) + .bind::>(g) } #[test] diff --git a/tests/test_functor.rs b/tests/test_functor.rs index 9281112..9667be1 100644 --- a/tests/test_functor.rs +++ b/tests/test_functor.rs @@ -1,11 +1,12 @@ #[cfg(test)] mod tests_map { + use maitar::specs::functor::Functor; use maitar::standard::option::OptionK; use maitar::standard::result::ResultK; use maitar::standard::vec::VecK; - fn test_map(ma: This::T) -> This::T { + fn test_map<'a, This: Functor<'a>>(ma: This::T) -> This::T { This::map(|a| a + 1, ma) } @@ -42,7 +43,7 @@ mod tests_map { mod infix { use maitar::specs::functor::infix::Functor; - fn test_map = This>>(ma: This) -> This::T { + fn test_map<'a, This: Functor<'a, i32, TL = This>>(ma: This) -> This::T { ma.map(|a| a - 1).map(|a| a + 2) } diff --git a/tests/test_monad.rs b/tests/test_monad.rs index 3cd5632..b2b4577 100644 --- a/tests/test_monad.rs +++ b/tests/test_monad.rs @@ -1,10 +1,11 @@ #[cfg(test)] mod tests_returns { + use maitar::specs::monad::Monad; use maitar::standard::option::OptionK; use maitar::standard::result::ResultK; - fn test_join(mma: This::T>) -> This::T { + fn test_join<'a, This: Monad<'a>>(mma: This::T>) -> This::T { This::join(mma) } @@ -58,7 +59,7 @@ mod tests_bind { use maitar::standard::result::ResultK; use maitar::standard::vec::VecK; - fn test_bind(ma: This::T) -> This::T { + fn test_bind<'a, This: Monad<'a>>(ma: This::T) -> This::T { This::bind(ma, |a| This::returns(a + 1)) } @@ -95,7 +96,7 @@ mod tests_bind { mod infix { use maitar::specs::monad::infix::Monad; - fn test_bind_with_f = This>>(ma: This) -> This::T { + fn test_bind_with_f<'a, This: Monad<'a, i32, TL = This>>(ma: This) -> This::T { ma.bind::(move |a| This::returns(a - 1)) .bind::(move |a| This::returns(a + 2)) }