Skip to content

Commit

Permalink
Rename modules for reader and writer since these are transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
d-plaindoux committed Dec 14, 2023
1 parent 9ca174d commit 763461d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
62 changes: 31 additions & 31 deletions src/standard/reader.rs → src/standard/reader_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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<dyn FnOnce(E) -> <F as HKP<'a>>::T<A> + 'a>,
PhantomData<F>,
);

pub struct ReaderK<'a, E, F: HKP<'a>>(PhantomData<&'a E>, PhantomData<F>);

impl<'a, E, F: HKP<'a>> HKP<'a> for ReaderK<'a, E, F> {
type T<B: 'a> = Reader<'a, E, F, B>;
type T<B: 'a> = ReaderT<'a, E, F, B>;
}

impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> {
Expand All @@ -28,25 +28,25 @@ impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> {
MAP: Fn(A) -> B + 'e,
{
let run = |e| F::map::<A, B, 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: 'e>(a: A) -> Self::T<A> {
let run = |_| F::pure(a);
Reader(Box::new(run), PhantomData)
ReaderT(Box::new(run), PhantomData)
}

fn apply<A, B, MAP>(mf: Self::T<MAP>, ma: Self::T<A>) -> Self::T<B>
where
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)
}
}

Expand All @@ -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<B> + '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<A>(f: fn(E) -> F::T<A>) -> Reader<'e, E, F, A> {
Reader(Box::new(f), PhantomData)
pub fn reader<A>(f: fn(E) -> F::T<A>) -> ReaderT<'e, E, F, A> {
ReaderT(Box::new(f), PhantomData)
}

pub fn run<A>(reader: Reader<'e, E, F, A>) -> Box<dyn FnOnce(E) -> F::T<A> + 'e> {
let Reader(f, _) = reader;
pub fn run<A>(reader: ReaderT<'e, E, F, A>) -> Box<dyn FnOnce(E) -> F::T<A> + '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<A>(f: Box<dyn Fn(E) -> 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<A>(f: Box<dyn Fn(E) -> 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)
}
}

Expand All @@ -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<B: 'a> = Reader<'a, E, F, B>;
impl<'a, E, F: HKP<'a>, A: 'a> HKP<'a> for ReaderT<'a, E, F, A> {
type T<B: 'a> = 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<B: 'a>(a: <Self::This as HKP<'a>>::T<B>) -> Self::T<B> {
Expand All @@ -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<B: 'a> = Reader<'a, E, F, B>;
type TL<B: 'a> = 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<B: 'a> = Reader<'a, E, F, B>;
type TL<B: 'a> = 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<B: 'a> = Reader<'a, E, F, B>;
type TL<B: 'a> = 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<B: 'a> = Reader<'a, E, F, B>;
type TL<B: 'a> = ReaderT<'a, E, F, B>;
}
}
14 changes: 7 additions & 7 deletions src/standard/writer.rs → src/standard/writer_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ 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<F>);

impl<'a, E, F: HKP<'a>> HKP<'a> for WriterK<'a, E, F> {
type T<B: 'a> = Writer<'a, E, F, B>;
type T<B: 'a> = WriterT<'a, E, F, B>;
}

impl<'a, E, F: Functor<'a>> Functor<'a> for WriterK<'a, E, F> {
fn map<A, B, MAP>(f: MAP, ma: Self::T<A>) -> Self::T<B>
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<T = E> + Copy, F: Applicative<'a> + 'a> Applicative<'a> for WriterK<'a, E, F> {
fn pure<A>(a: A) -> Self::T<A> {
Writer(F::pure((E::neutral(), a)))
WriterT(F::pure((E::neutral(), a)))
}

fn apply<A, B, M>(mf: Self::T<M>, ma: Self::T<A>) -> Self::T<B>
where
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,
Expand All @@ -53,7 +53,7 @@ impl<'a, E: Monoid<T = E> + Copy, F: Bind<'a> + 'a> Bind<'a> for WriterK<'a, E,
where
BIND: Fn(A) -> Self::T<B> + '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)
}))
}
Expand Down

0 comments on commit 763461d

Please sign in to comment.