FunctorContext

This commit is contained in:
AF 2023-05-30 15:00:28 +00:00
parent c565f0b6dc
commit 6ec6ba7076
5 changed files with 61 additions and 31 deletions

View File

@ -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<<BT as BinaryTrees<'a>>::Key>;
@ -14,16 +14,16 @@ pub type Split<'a, BT> = (
);
pub type KeySplit<'a, BT> = (<BT as BinaryTrees<'a>>::Tree, <BT as BinaryTrees<'a>>::Tree);
pub type BTWrap<'a, BT, A> = Wrap<'a, A, <BT as BinaryTrees<'a>>::T>;
pub type BTWrap<'a, BT, A> = Wrap<'a, A, <BT as FunctorContext<'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<Self::Key>;
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<Self::Reference>;
fn bind<A: 'a, B: 'a>(
fa: BTWrap<'a, Self, A>,
f: impl 'a + FnOnce(A) -> BTWrap<'a, Self, B>,
) -> BTWrap<'a, Self, B> {
<Self::T as Monad>::bind(fa, f)
}
fn pure<A: 'a>(a: A) -> BTWrap<'a, Self, A> {
<Self::T as Pure>::pure(a)
}
fn fmap<A: 'a, B: 'a>(
fa: BTWrap<'a, Self, A>,
f: impl 'a + FnOnce(A) -> B,
) -> BTWrap<'a, Self, B> {
<Self::T as Functor>::fmap(fa, f)
}
fn fail<A: 'a, E: 'a>(e: E) -> BTWrap<'a, Self, A>
where
Self::T: Fail<'a, E>,
{
<Self::T as Fail<_>>::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))
}

View File

@ -108,6 +108,10 @@ impl<A> Clone for AvlTs<A> {
}
}
impl<'a, A: 'a> FunctorContext<'a> for AvlTs<A> {
type T = instances::solo::SoloInstance;
}
impl<'a, A: 'a + PartialOrd> BinaryTrees<'a> for AvlTs<A> {
type Node = AvlN<A>;
@ -119,7 +123,7 @@ impl<'a, A: 'a + PartialOrd> BinaryTrees<'a> for AvlTs<A> {
type Comparator = DefaultComparator;
type T = instances::solo::SoloInstance;
type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator {
&DefaultComparator

View File

@ -9,6 +9,7 @@
mod applicative_select;
pub mod clone_func;
pub mod context;
mod controlflow;
pub mod copy_func;
pub mod derivations;

43
src/func/context.rs Normal file
View File

@ -0,0 +1,43 @@
use super::*;
pub trait FunctorContext<'a>: 'a {
type T: WeakFunctor<'a>;
}
pub trait FunctorContextExt<'a>: FunctorContext<'a> {
fn fmap<A: 'a, B: 'a>(
fa: Wrap<'a, A, Self::T>,
f: impl 'a + FnOnce(A) -> B,
) -> Wrap<'a, B, Self::T>
where
Self::T: Functor<'a>,
{
<Self::T as Functor>::fmap(fa, f)
}
fn pure<A: 'a>(a: A) -> Wrap<'a, A, Self::T>
where
Self::T: Pure<'a>,
{
<Self::T as Pure>::pure(a)
}
fn bind<A: 'a, B: 'a>(
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>,
{
<Self::T as Monad>::bind(fa, f)
}
fn fail<A: 'a, E: 'a>(e: E) -> Wrap<'a, A, Self::T>
where
Self::T: Fail<'a, E>,
{
<Self::T as Fail<_>>::fail(e)
}
}
impl<'a, Ctx: FunctorContext<'a>> FunctorContextExt<'a> for Ctx {}

View File

@ -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<A>> Clone
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> 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<A>> BinaryTrees<'a>
for BoundContext<'a, Ctx, A, C>
{
@ -29,7 +36,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> 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