From 6ec6ba70764f91e703ea9cffd2ed78eb27629343 Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 30 May 2023 15:00:28 +0000 Subject: [PATCH] `FunctorContext` --- src/flow/binary.rs | 33 +++------------------- src/flow/binary/avl.rs | 6 +++- src/func.rs | 1 + src/func/context.rs | 43 +++++++++++++++++++++++++++++ src/rstd/collections/avl/context.rs | 9 +++++- 5 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 src/func/context.rs diff --git a/src/flow/binary.rs b/src/flow/binary.rs index f86182e..9e5ef58 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -3,7 +3,7 @@ mod avl; use std::rc::Rc; use crate::flow::comparator::*; -use crate::func::*; +use crate::func::{context::*, *}; pub type KeyRc<'a, BT> = Rc<>::Key>; @@ -14,16 +14,16 @@ pub type Split<'a, BT> = ( ); pub type KeySplit<'a, BT> = (>::Tree, >::Tree); -pub type BTWrap<'a, BT, A> = Wrap<'a, A, >::T>; +pub type BTWrap<'a, BT, A> = Wrap<'a, A, >::T>; -pub trait BinaryTrees<'a>: 'a + Clone { +pub trait BinaryTrees<'a>: FunctorContext<'a, T = Self::_Tm> + Clone { type Node: 'a; type Reference: 'a; type Tree: 'a; type Key: 'a; type Comparator: Comparator; - type T: Monad<'a>; + type _Tm: Monad<'a>; fn comparator(&self) -> &Self::Comparator; fn split(&self, node: &Self::Node) -> Split<'a, Self>; @@ -32,31 +32,6 @@ pub trait BinaryTrees<'a>: 'a + Clone { fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool; fn refer(&self, tree: &Self::Tree) -> Option; - fn bind( - fa: BTWrap<'a, Self, A>, - f: impl 'a + FnOnce(A) -> BTWrap<'a, Self, B>, - ) -> BTWrap<'a, Self, B> { - ::bind(fa, f) - } - - fn pure(a: A) -> BTWrap<'a, Self, A> { - ::pure(a) - } - - fn fmap( - fa: BTWrap<'a, Self, A>, - f: impl 'a + FnOnce(A) -> B, - ) -> BTWrap<'a, Self, B> { - ::fmap(fa, f) - } - - fn fail(e: E) -> BTWrap<'a, Self, A> - where - Self::T: Fail<'a, E>, - { - >::fail(e) - } - fn tree_bind(self, fnode: BTWrap<'a, Self, Self::Node>) -> BTWrap<'a, Self, Self::Tree> { Self::bind(fnode, move |node| self.tree_of(node)) } diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index edeb41f..1235f73 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -108,6 +108,10 @@ impl Clone for AvlTs { } } +impl<'a, A: 'a> FunctorContext<'a> for AvlTs { + type T = instances::solo::SoloInstance; +} + impl<'a, A: 'a + PartialOrd> BinaryTrees<'a> for AvlTs { type Node = AvlN; @@ -119,7 +123,7 @@ impl<'a, A: 'a + PartialOrd> BinaryTrees<'a> for AvlTs { type Comparator = DefaultComparator; - type T = instances::solo::SoloInstance; + type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { &DefaultComparator diff --git a/src/func.rs b/src/func.rs index b1062c2..e2492cc 100644 --- a/src/func.rs +++ b/src/func.rs @@ -9,6 +9,7 @@ mod applicative_select; pub mod clone_func; +pub mod context; mod controlflow; pub mod copy_func; pub mod derivations; diff --git a/src/func/context.rs b/src/func/context.rs new file mode 100644 index 0000000..f3e69c4 --- /dev/null +++ b/src/func/context.rs @@ -0,0 +1,43 @@ +use super::*; + +pub trait FunctorContext<'a>: 'a { + type T: WeakFunctor<'a>; +} + +pub trait FunctorContextExt<'a>: FunctorContext<'a> { + fn fmap( + fa: Wrap<'a, A, Self::T>, + f: impl 'a + FnOnce(A) -> B, + ) -> Wrap<'a, B, Self::T> + where + Self::T: Functor<'a>, + { + ::fmap(fa, f) + } + + fn pure(a: A) -> Wrap<'a, A, Self::T> + where + Self::T: Pure<'a>, + { + ::pure(a) + } + + fn bind( + fa: Wrap<'a, A, Self::T>, + f: impl 'a + FnOnce(A) -> Wrap<'a, B, Self::T>, + ) -> Wrap<'a, B, Self::T> + where + Self::T: Monad<'a>, + { + ::bind(fa, f) + } + + fn fail(e: E) -> Wrap<'a, A, Self::T> + where + Self::T: Fail<'a, E>, + { + >::fail(e) + } +} + +impl<'a, Ctx: FunctorContext<'a>> FunctorContextExt<'a> for Ctx {} diff --git a/src/rstd/collections/avl/context.rs b/src/rstd/collections/avl/context.rs index 4d2f047..ca1c0b3 100644 --- a/src/rstd/collections/avl/context.rs +++ b/src/rstd/collections/avl/context.rs @@ -1,4 +1,5 @@ use crate::flow::{binary::*, comparator::*}; +use crate::func::context::*; use crate::func::*; use crate::rstd::fallible::*; @@ -20,6 +21,12 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator> Clone } } +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator> FunctorContext<'a> + for BoundContext<'a, Ctx, A, C> +{ + type T = FallibleMonad<'a, Ctx, BoundResolutionError<'a, Ctx, A>>; +} + impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator> BinaryTrees<'a> for BoundContext<'a, Ctx, A, C> { @@ -29,7 +36,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator> Binar type Key = A; type Comparator = C; - type T = FallibleMonad<'a, Ctx, BoundResolutionError<'a, Ctx, A>>; + type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { &self.comparator