Skip to content

Commit

Permalink
Remove some type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
d-plaindoux committed Sep 18, 2023
1 parent 19b8119 commit 6b5e2d6
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Some incarnations are available like:
- Reader and
- Writer.

Note: Curried version is not yet optimal since it relies on Boxed using polymorphic functions (dynamic dispatch).

# Philosophy

In Rust, we can specify abstract types in a trait, i.e. Higher-Kinded Polymorphism
Expand Down
14 changes: 7 additions & 7 deletions src/core/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
* Copyright (c) 2023 Didier Plaindoux
*/

use crate::core::types::FunOnceLT;

pub fn uncurry<'a, A, B, C, F>(f: F) -> Box<dyn Fn(A, B) -> C + 'a>
pub fn uncurry<'a, A, B, C, F>(f: F) -> impl Fn(A, B) -> C + 'a
where
F: Fn(A) -> Box<dyn Fn(B) -> C> + 'a,
{
Box::new(move |a, b| f(a)(b))
}

pub fn curry<'a, A, B, C, F>(f: F) -> FunOnceLT<'a, A, FunOnceLT<'a, B, C>>
pub fn curry<'a, A, B, C, F>(f: F) -> impl FnOnce(A) -> Box<dyn FnOnce(B) -> C + 'a>
where
A: 'a,
F: FnOnce(A, B) -> C + 'a,
{
Box::new(move |a| Box::new(move |b| f(a, b)))
|a| Box::new(move |b| f(a, b))
}

pub fn curry3<'a, A, B, C, D, F>(f: F) -> FunOnceLT<'a, A, FunOnceLT<'a, B, FunOnceLT<'a, C, D>>>
pub fn curry3<'a, A, B, C, D, F>(
f: F,
) -> impl FnOnce(A) -> Box<dyn FnOnce(B) -> Box<dyn FnOnce(C) -> D + 'a> + 'a>
where
A: 'a,
B: 'a,
F: FnOnce(A, B, C) -> D + 'a,
{
Box::new(move |a| Box::new(move |b| Box::new(move |c| f(a, b, c))))
|a| Box::new(move |b| Box::new(move |c| f(a, b, c)))
}
1 change: 0 additions & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
pub mod functions;
pub mod hkp;
pub mod transform;
pub mod types;
12 changes: 0 additions & 12 deletions src/core/types.rs

This file was deleted.

7 changes: 3 additions & 4 deletions src/specs/applicative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ pub trait Applicative<'a>: Functor<'a> {

pub mod curry {
use crate::core::functions::{curry, curry3};
use crate::core::types::FunOnceLT;
use crate::specs::applicative::Applicative as Api;

pub trait Applicative<'a>: Api<'a> {
fn apply<A, B, F>(mf: Self::T<F>) -> FunOnceLT<'a, Self::T<A>, Self::T<B>>
fn apply<A, B, F>(mf: Self::T<F>) -> Box<dyn FnOnce(Self::T<A>) -> Self::T<B> + 'a>
where
Self: 'a,
A: Clone,
Expand All @@ -46,7 +45,7 @@ pub mod curry {
curry(<Self as Api<'a>>::apply)(mf)
}

fn lift1<A, B, F>(f: F) -> FunOnceLT<'a, Self::T<A>, Self::T<B>>
fn lift1<A, B, F>(f: F) -> Box<dyn FnOnce(Self::T<A>) -> Self::T<B> + 'a>
where
Self: 'a,
F: Fn(A) -> B + 'a,
Expand All @@ -56,7 +55,7 @@ pub mod curry {

fn lift2<A, B, C, F>(
f: F,
) -> FunOnceLT<'a, Self::T<A>, FunOnceLT<'a, Self::T<B>, Self::T<C>>>
) -> Box<dyn FnOnce(Self::T<A>) -> Box<dyn FnOnce(Self::T<B>) -> Self::T<C> + 'a> + 'a>
where
Self: 'a,
A: Clone,
Expand Down
3 changes: 1 addition & 2 deletions src/specs/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ pub trait Bind<'a>: Applicative<'a> {

pub mod curry {
use crate::core::functions::curry;
use crate::core::types::FunOnceLT;
use crate::specs::bind::Bind as Api;

pub trait Bind<'a>: Api<'a> {
fn bind<A, B, BIND>(ma: Self::T<A>) -> FunOnceLT<'a, BIND, Self::T<B>>
fn bind<A, B, BIND>(ma: Self::T<A>) -> Box<dyn FnOnce(BIND) -> Self::T<B> + 'a>
where
Self: 'a,
BIND: Fn(A) -> Self::T<B> + 'a,
Expand Down
1 change: 0 additions & 1 deletion src/specs/functor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod curry {
fn map<A, B, MAP>(f: MAP) -> Box<dyn FnOnce(Self::T<A>) -> Self::T<B> + 'a>
where
Self: 'a,
A: Copy,
MAP: Fn(A) -> B + 'a,
{
curry(<Self as Api<'a>>::map)(f)
Expand Down
9 changes: 5 additions & 4 deletions src/standard/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*/

use crate::core::hkp::HKP;
use crate::core::types::FunOnceLT;
use crate::specs::applicative::Applicative;
use crate::specs::bind::Bind;
use crate::specs::functor::Functor;
use crate::specs::monad::Monad;
use std::marker::PhantomData;

pub struct Reader<'a, E, F: HKP<'a>, A: 'a>(FunOnceLT<'a, E, F::T<A>>, PhantomData<F>);
pub struct Reader<'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>);

Expand All @@ -25,8 +27,7 @@ impl<'e, E, F: Functor<'e> + 'e> Functor<'e> for ReaderK<'e, E, F> {
where
MAP: Fn(A) -> B + 'e,
{
let Reader(va, _) = ma;
let run = |e| F::map::<A, B, MAP>(f, va(e));
let run = |e| F::map::<A, B, MAP>(f, ma.0(e));
Reader(Box::new(run), PhantomData)
}
}
Expand Down

0 comments on commit 6b5e2d6

Please sign in to comment.