From 0954c86e928c54f4bda9367f142784b51de6f7f9 Mon Sep 17 00:00:00 2001 From: timofey Date: Thu, 31 Aug 2023 23:56:38 +0000 Subject: [PATCH] `MonadContext` --- src/flow/binary.rs | 4 +--- src/flow/binary/balancing.rs | 6 ++++-- src/flow/binary/bound.rs | 6 ++++-- src/func/context.rs | 4 ++++ src/mrds/trees/heighted.rs | 6 ++++-- src/rcore.rs | 3 ++- src/rcore/context.rs | 5 +---- src/rstd/collections/tree/context.rs | 13 +++++++++++-- src/testing.rs | 4 +++- src/testing/counted.rs | 4 +++- src/testing/traced.rs | 4 +++- 11 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/flow/binary.rs b/src/flow/binary.rs index ffbfd49..7263bf5 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -30,9 +30,7 @@ pub trait BinaryTrees: Clone { fn refer(&self, tree: &Self::Tree) -> Option; } -pub trait MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees { - type _Tm: Monad<'a>; - +pub trait MonadTrees<'a>: MonadContext<'a> + BinaryTrees { fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>; } diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index 967d212..928374d 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -37,6 +37,10 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BalancedTrees { type T = BT::T; } +impl<'a, BT: MonadContext<'a>> MonadContext<'a> for BalancedTrees { + type _Tm = Self::T; +} + #[derive(Debug)] pub enum BalancingError { Height(HeightError), @@ -109,8 +113,6 @@ impl BinaryTrees for BalancedTrees { } impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { - type _Tm = Self::T; - fn resolve(&self, (reference, hp): &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let hp = *hp; let ctx = self.0.clone(); diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index e3f86b1..86c297c 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -43,6 +43,10 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees { type T = BT::T; } +impl<'a, BT: MonadContext<'a>> MonadContext<'a> for BoundTrees { + type _Tm = Self::T; +} + pub trait BinaryTreesBindable<'a>: MonadTrees<'a> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T>; @@ -98,8 +102,6 @@ impl BinaryTrees for BoundTrees { } impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { - type _Tm = Self::T; - fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let ctx = self.0.clone(); let bounds = reference.bounds.clone(); diff --git a/src/func/context.rs b/src/func/context.rs index 5f658ac..7a42260 100644 --- a/src/func/context.rs +++ b/src/func/context.rs @@ -6,6 +6,10 @@ pub trait FunctorContext<'a>: 'a + Send { type T: WeakFunctor<'a>; } +pub trait MonadContext<'a>: FunctorContext<'a, T = Self::_Tm> { + type _Tm: Monad<'a>; +} + pub trait FunctorContextExt<'a>: FunctorContext<'a> { fn fmap( fa: WrapC<'a, A, Self>, diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index bce7986..4736559 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -86,6 +86,10 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees { type T = instances::solo::SoloInstance; } +impl<'a, A: 'a + Send> MonadContext<'a> for Trees { + type _Tm = Self::T; +} + impl BinaryTrees for Trees { type Node = Node; type Reference = Reference; @@ -111,8 +115,6 @@ impl BinaryTrees for Trees { } impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees { - type _Tm = Self::T; - fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { reference.node.as_ref().clone() } diff --git a/src/rcore.rs b/src/rcore.rs index 2365fc0..5367243 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -21,7 +21,6 @@ mod resolver_origin; use std::{error::Error, sync::Arc}; use crate::func::context::*; -use crate::func::*; use crate::mode::*; pub use self::context::Context; @@ -43,6 +42,8 @@ pub use self::resolution::{ }; /// Helper alias for [`WeakFunctor::F`] of [`FunctorContext::T`]. +/// +/// [`WeakFunctor::F`]: crate::func::WeakFunctor::F pub type Wrapped<'a, Ctx, A> = WrapC<'a, A, Ctx>; /// [Mentionable] base. diff --git a/src/rcore/context.rs b/src/rcore/context.rs index f43f36e..3310203 100644 --- a/src/rcore/context.rs +++ b/src/rcore/context.rs @@ -1,10 +1,7 @@ use super::*; /// Execution context. -pub trait Context<'a>: FallibleCtx<'a, T = Self::_Tm> + Send + Sync { - /// Type to provide for [Monad]ic representation of computation, mostly that of resolution ([`Resolution`]). - type _Tm: Monad<'a>; - +pub trait Context<'a>: FallibleCtx<'a> + MonadContext<'a> + Send + Sync { /// See [`Diagnostic`]. type D: Diagnostic<'a, Self::T>; diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index d7653bd..7a79bdf 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -69,6 +69,17 @@ impl< type T = FallibleMonad<'a, Ctx, TreeContextError<'a, Ctx, A, E>>; } +impl< + 'a, + Ctx: Context<'a>, + A: Mentionable<'a, Ctx> + Clone, + C: 'a + Comparator, + E: 'a + Send, + > MonadContext<'a> for TreeContext2<'a, Ctx, A, C, E> +{ + type _Tm = Self::T; +} + impl< 'a, Ctx: Context<'a>, @@ -108,8 +119,6 @@ impl< E: 'a + Send, > MonadTrees<'a> for TreeContext2<'a, Ctx, A, C, E> { - type _Tm = Self::T; - fn resolve( &self, reference: &Self::Reference, diff --git a/src/testing.rs b/src/testing.rs index f0e3c33..b120b32 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -72,9 +72,11 @@ impl<'a> FallibleCtx<'a> for TestContextPlain { type Fallible = instances::result::ResultFailAny; } -impl<'a> Context<'a> for TestContextPlain { +impl<'a> MonadContext<'a> for TestContextPlain { type _Tm = Self::T; +} +impl<'a> Context<'a> for TestContextPlain { type D = NoDiagnostic; type LookupError = TestLookupError<'a>; diff --git a/src/testing/counted.rs b/src/testing/counted.rs index 87a5d7b..b42c6ae 100644 --- a/src/testing/counted.rs +++ b/src/testing/counted.rs @@ -15,9 +15,11 @@ impl<'a> FallibleCtx<'a> for TestContextCounted { type Fallible = instances::result::ResultFailOver; } -impl<'a> Context<'a> for TestContextCounted { +impl<'a> MonadContext<'a> for TestContextCounted { type _Tm = Self::T; +} +impl<'a> Context<'a> for TestContextCounted { type D = NoDiagnostic; type LookupError = TestLookupError<'a>; diff --git a/src/testing/traced.rs b/src/testing/traced.rs index 29f4829..6049df4 100644 --- a/src/testing/traced.rs +++ b/src/testing/traced.rs @@ -14,9 +14,11 @@ impl<'a> FallibleCtx<'a> for TestContextTraced { type Fallible = instances::result::ResultFailOver; } -impl<'a> Context<'a> for TestContextTraced { +impl<'a> MonadContext<'a> for TestContextTraced { type _Tm = Self::T; +} +impl<'a> Context<'a> for TestContextTraced { type D = TracedDiagnostic; type LookupError = TestLookupError<'a>;