From 4ad05fbe7c33ca4e1fe1bcdaffe632743bf37869 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 21 Apr 2023 18:14:31 +0000 Subject: [PATCH] move derivations into a separate module --- src/func.rs | 23 +------------ src/func/classes/stackless.rs | 9 +++-- src/func/derivations.rs | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 src/func/derivations.rs diff --git a/src/func.rs b/src/func.rs index 176c4c3..dd0d4ef 100644 --- a/src/func.rs +++ b/src/func.rs @@ -1,6 +1,7 @@ pub mod classes; pub mod clone_func; pub mod copy_func; +pub mod derivations; #[cfg(test)] pub mod test_suite; #[cfg(test)] @@ -85,16 +86,6 @@ pub trait ApplicativeSeq: Functor { ) -> Self::F<'a, B> where Self: 'a; - fn _la2<'a, A: 'a, B: 'a, C: 'a>( - f: impl 'a + FnOnce(A, B) -> C, - fa: Self::F<'a, A>, - fb: Self::F<'a, B>, - ) -> Self::F<'a, C> - where - Self: 'a, - { - Self::seq(Self::fmap(|a| |b| f(a, b), fa), fb) - } } pub trait ApplicativeLA2: Functor { @@ -105,18 +96,6 @@ pub trait ApplicativeLA2: Functor { ) -> Self::F<'a, C> where Self: 'a; - fn _seq<'a, A: 'a, B: 'a>( - ff: Self::F<'a, impl 'a + FnOnce(A) -> B>, - fa: Self::F<'a, A>, - ) -> Self::F<'a, B> - where - Self: 'a, - { - Self::la2(|f, a| f(a), ff, fa) - } - fn _tuple<'a, A: 'a, B: 'a>((fa, fb): (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)> { - Self::la2(|a, b| (a, b), fa, fb) - } } pub trait ApplicativeTuple: Functor { diff --git a/src/func/classes/stackless.rs b/src/func/classes/stackless.rs index 63f3be4..f7a96ae 100644 --- a/src/func/classes/stackless.rs +++ b/src/func/classes/stackless.rs @@ -1,5 +1,6 @@ use std::{cell::Cell, rc::Rc}; +use crate::func::derivations::*; use crate::func::*; enum EvalTree<'a> { @@ -29,9 +30,7 @@ impl<'a> EvalTree<'a> { type StackessDyn<'a, A> = dyn 'a + FnOnce(Box) -> Oet<'a>; -pub struct Stackless<'a, A: 'a>( - Box>, -); +pub struct Stackless<'a, A: 'a>(Box>); fn set_cell(cell: Rc>>, a: A) { if cell.replace(Some(a)).is_some() { @@ -159,7 +158,7 @@ impl ApplicativeLA2 for StacklessClass { where Self: 'a, { - Self::_la2(f, fa, fb) + Self::_la2_via_seq(f, fa, fb) } } @@ -168,7 +167,7 @@ impl ApplicativeTuple for StacklessClass { where Self: 'a, { - Self::_tuple((fa, fb)) + Self::_tuple_via_la2((fa, fb)) } } diff --git a/src/func/derivations.rs b/src/func/derivations.rs new file mode 100644 index 0000000..461e04a --- /dev/null +++ b/src/func/derivations.rs @@ -0,0 +1,64 @@ +use crate::func::*; + +pub trait ApplicativeLA2ViaSeq: ApplicativeSeq { + fn _la2_via_seq<'a, A: 'a, B: 'a, C: 'a>( + f: impl 'a + FnOnce(A, B) -> C, + fa: Self::F<'a, A>, + fb: Self::F<'a, B>, + ) -> Self::F<'a, C> + where + Self: 'a; +} + +impl ApplicativeLA2ViaSeq for T { + fn _la2_via_seq<'a, A: 'a, B: 'a, C: 'a>( + f: impl 'a + FnOnce(A, B) -> C, + fa: Self::F<'a, A>, + fb: Self::F<'a, B>, + ) -> Self::F<'a, C> + where + Self: 'a, + { + Self::seq(Self::fmap(|a| |b| f(a, b), fa), fb) + } +} + +pub trait ApplicativeSeqViaLA2: ApplicativeLA2 { + fn _seq_via_la2<'a, A: 'a, B: 'a>( + ff: Self::F<'a, impl 'a + FnOnce(A) -> B>, + fa: Self::F<'a, A>, + ) -> Self::F<'a, B> + where + Self: 'a; +} + +impl ApplicativeSeqViaLA2 for T { + fn _seq_via_la2<'a, A: 'a, B: 'a>( + ff: Self::F<'a, impl 'a + FnOnce(A) -> B>, + fa: Self::F<'a, A>, + ) -> Self::F<'a, B> + where + Self: 'a, + { + Self::la2(|f, a| f(a), ff, fa) + } +} + +pub trait ApplicativeTupleViaLA2: ApplicativeLA2 { + fn _tuple_via_la2<'a, A: 'a, B: 'a>( + fab: (Self::F<'a, A>, Self::F<'a, B>), + ) -> Self::F<'a, (A, B)> + where + Self: 'a; +} + +impl ApplicativeTupleViaLA2 for T { + fn _tuple_via_la2<'a, A: 'a, B: 'a>( + (fa, fb): (Self::F<'a, A>, Self::F<'a, B>), + ) -> Self::F<'a, (A, B)> + where + Self: 'a, + { + Self::la2(|a, b| (a, b), fa, fb) + } +}