From 763461d720a21b670bda060210f760c6272f1b90 Mon Sep 17 00:00:00 2001 From: d-plaindoux Date: Thu, 14 Dec 2023 05:49:26 +0100 Subject: [PATCH] Rename modules for reader and writer since these are transformers --- src/standard/mod.rs | 4 +- src/standard/{reader.rs => reader_t.rs} | 62 ++++++++++++------------- src/standard/{writer.rs => writer_t.rs} | 14 +++--- 3 files changed, 40 insertions(+), 40 deletions(-) rename src/standard/{reader.rs => reader_t.rs} (66%) rename src/standard/{writer.rs => writer_t.rs} (82%) diff --git a/src/standard/mod.rs b/src/standard/mod.rs index 3a03942..12896d1 100644 --- a/src/standard/mod.rs +++ b/src/standard/mod.rs @@ -7,7 +7,7 @@ pub mod either; pub mod identity; pub mod option; -pub mod reader; +pub mod reader_t; pub mod result; pub mod vec; -pub mod writer; +pub mod writer_t; diff --git a/src/standard/reader.rs b/src/standard/reader_t.rs similarity index 66% rename from src/standard/reader.rs rename to src/standard/reader_t.rs index 5b3c1b3..5c61d4a 100644 --- a/src/standard/reader.rs +++ b/src/standard/reader_t.rs @@ -11,7 +11,7 @@ use crate::specs::functor::Functor; use crate::specs::monad::Monad; use std::marker::PhantomData; -pub struct Reader<'a, E, F: HKP<'a>, A: 'a>( +pub struct ReaderT<'a, E, F: HKP<'a>, A: 'a>( Box >::T + 'a>, PhantomData, ); @@ -19,7 +19,7 @@ pub struct Reader<'a, E, F: HKP<'a>, A: '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>; + type T = ReaderT<'a, E, F, B>; } impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> { @@ -28,14 +28,14 @@ impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> { MAP: Fn(A) -> B + 'e, { let run = |e| F::map::(f, ma.0(e)); - Reader(Box::new(run), PhantomData) + ReaderT(Box::new(run), PhantomData) } } impl<'e, E: Copy, F: Applicative<'e> + 'e> Applicative<'e> for ReaderK<'e, E, F> { fn pure(a: A) -> Self::T { let run = |_| F::pure(a); - Reader(Box::new(run), PhantomData) + ReaderT(Box::new(run), PhantomData) } fn apply(mf: Self::T, ma: Self::T) -> Self::T @@ -43,10 +43,10 @@ impl<'e, E: Copy, F: Applicative<'e> + 'e> Applicative<'e> for ReaderK<'e, E, F> A: Clone, MAP: Fn(A) -> B + Clone, { - let Reader(vf, _) = mf; - let Reader(va, _) = ma; + let ReaderT(vf, _) = mf; + let ReaderT(va, _) = ma; let run = |e: E| F::apply(vf(e), va(e)); - Reader(Box::new(run), PhantomData) + ReaderT(Box::new(run), PhantomData) } } @@ -55,37 +55,37 @@ impl<'e, E: Copy, F: Bind<'e> + 'e> Bind<'e> for ReaderK<'e, E, F> { where BIND: Fn(A) -> Self::T + 'e, { - let Reader(va, _) = ma; + let ReaderT(va, _) = ma; let run = |e1: E| { F::bind(va(e1), move |e2: A| { - let Reader(v, _) = f(e2); + let ReaderT(v, _) = f(e2); v(e1) }) }; - Reader(Box::new(run), PhantomData) + ReaderT(Box::new(run), PhantomData) } } impl<'e, E: Copy, F: Bind<'e> + 'e> Monad<'e> for ReaderK<'e, E, F> {} impl<'e, E: Copy + 'e, F: Monad<'e> + 'e> ReaderK<'e, E, F> { - pub fn reader(f: fn(E) -> F::T) -> Reader<'e, E, F, A> { - Reader(Box::new(f), PhantomData) + pub fn reader(f: fn(E) -> F::T) -> ReaderT<'e, E, F, A> { + ReaderT(Box::new(f), PhantomData) } - pub fn run(reader: Reader<'e, E, F, A>) -> Box F::T + 'e> { - let Reader(f, _) = reader; + pub fn run(reader: ReaderT<'e, E, F, A>) -> Box F::T + 'e> { + let ReaderT(f, _) = reader; f } - pub fn ask() -> Reader<'e, E, F, E> { - Reader(Box::new(F::returns), PhantomData) + pub fn ask() -> ReaderT<'e, E, F, E> { + ReaderT(Box::new(F::returns), PhantomData) } - pub fn local(f: Box E>, reader: Reader<'e, E, F, A>) -> Reader<'e, E, F, A> { - let Reader(run, _) = reader; - Reader(Box::new(move |e| run(f(e))), PhantomData) + pub fn local(f: Box E>, reader: ReaderT<'e, E, F, A>) -> ReaderT<'e, E, F, A> { + let ReaderT(run, _) = reader; + ReaderT(Box::new(move |e| run(f(e))), PhantomData) } } @@ -96,13 +96,13 @@ pub mod infix { use crate::specs::bind::infix::Bind; use crate::specs::functor::infix::Functor; use crate::specs::monad::infix::Monad; - use crate::standard::reader::{Reader, ReaderK}; + use crate::standard::reader_t::{ReaderK, ReaderT}; - impl<'a, E, F: HKP<'a>, A: 'a> HKP<'a> for Reader<'a, E, F, A> { - type T = Reader<'a, E, F, B>; + impl<'a, E, F: HKP<'a>, A: 'a> HKP<'a> for ReaderT<'a, E, F, A> { + type T = ReaderT<'a, E, F, B>; } - impl<'a, E: 'a, F: HKP<'a>, A: 'a> Transform<'a, A> for Reader<'a, E, F, A> { + impl<'a, E: 'a, F: HKP<'a>, A: 'a> Transform<'a, A> for ReaderT<'a, E, F, A> { type This = ReaderK<'a, E, F>; fn hkp_to_self(a: >::T) -> Self::T { @@ -119,30 +119,30 @@ pub mod infix { } impl<'a, E: 'a, F: HKP<'a> + crate::specs::functor::Functor<'a> + 'a, A: 'a> Functor<'a, A> - for Reader<'a, E, F, A> + for ReaderT<'a, E, F, A> { type ThisL = ReaderK<'a, E, F>; - type TL = Reader<'a, E, F, B>; + type TL = ReaderT<'a, E, F, B>; } impl<'a, E: 'a + Copy, F: HKP<'a> + crate::specs::applicative::Applicative<'a> + 'a, A: 'a> - Applicative<'a, A> for Reader<'a, E, F, A> + Applicative<'a, A> for ReaderT<'a, E, F, A> { type ThisL = ReaderK<'a, E, F>; - type TL = Reader<'a, E, F, B>; + type TL = ReaderT<'a, E, F, B>; } impl<'a, E: 'a + Copy, F: HKP<'a> + crate::specs::bind::Bind<'a> + 'a, A: 'a> Bind<'a, A> - for Reader<'a, E, F, A> + for ReaderT<'a, E, F, A> { type ThisL = ReaderK<'a, E, F>; - type TL = Reader<'a, E, F, B>; + type TL = ReaderT<'a, E, F, B>; } impl<'a, E: 'a + Copy, F: HKP<'a> + crate::specs::monad::Monad<'a> + 'a, A: 'a> Monad<'a, A> - for Reader<'a, E, F, A> + for ReaderT<'a, E, F, A> { type ThisL = ReaderK<'a, E, F>; - type TL = Reader<'a, E, F, B>; + type TL = ReaderT<'a, E, F, B>; } } diff --git a/src/standard/writer.rs b/src/standard/writer_t.rs similarity index 82% rename from src/standard/writer.rs rename to src/standard/writer_t.rs index 001be5f..0ea24b6 100644 --- a/src/standard/writer.rs +++ b/src/standard/writer_t.rs @@ -12,12 +12,12 @@ use crate::specs::monad::Monad; use crate::specs::monoid::Monoid; use std::marker::PhantomData; -pub struct Writer<'a, E: 'a, F: HKP<'a>, A: 'a>(F::T<(E, A)>); +pub struct WriterT<'a, E: 'a, F: HKP<'a>, A: 'a>(F::T<(E, A)>); pub struct WriterK<'a, E, F: HKP<'a>>(PhantomData<&'a E>, PhantomData); impl<'a, E, F: HKP<'a>> HKP<'a> for WriterK<'a, E, F> { - type T = Writer<'a, E, F, B>; + type T = WriterT<'a, E, F, B>; } impl<'a, E, F: Functor<'a>> Functor<'a> for WriterK<'a, E, F> { @@ -25,14 +25,14 @@ impl<'a, E, F: Functor<'a>> Functor<'a> for WriterK<'a, E, F> { where MAP: Fn(A) -> B + 'a, { - let Writer(va) = ma; - Writer(F::map(move |(e, a)| (e, f(a)), va)) + let WriterT(va) = ma; + WriterT(F::map(move |(e, a)| (e, f(a)), va)) } } impl<'a, E: Monoid + Copy, F: Applicative<'a> + 'a> Applicative<'a> for WriterK<'a, E, F> { fn pure(a: A) -> Self::T { - Writer(F::pure((E::neutral(), a))) + WriterT(F::pure((E::neutral(), a))) } fn apply(mf: Self::T, ma: Self::T) -> Self::T @@ -40,7 +40,7 @@ impl<'a, E: Monoid + Copy, F: Applicative<'a> + 'a> Applicative<'a> for W A: Clone, M: Fn(A) -> B + 'a + Clone, { - Writer(F::lift2( + WriterT(F::lift2( |(t, f)| move |(u, g)| (E::compose(t, u), f(g)), mf.0, ma.0, @@ -53,7 +53,7 @@ impl<'a, E: Monoid + Copy, F: Bind<'a> + 'a> Bind<'a> for WriterK<'a, E, where BIND: Fn(A) -> Self::T + 'a, { - Writer(F::bind(ma.0, move |(x, t)| { + WriterT(F::bind(ma.0, move |(x, t)| { F::map(move |(y, u)| (E::compose(x, y), u), mf(t).0) })) }