restructuring

This commit is contained in:
AF 2023-05-30 01:27:57 +00:00
parent ba28a7acef
commit 7b8221b4c3
8 changed files with 103 additions and 93 deletions

View File

@ -2,7 +2,8 @@ mod avl;
use std::rc::Rc;
use crate::{flow::comparator::*, func::*};
use crate::flow::comparator::*;
use crate::func::*;
pub type KeyRc<'a, BT> = Rc<<BT as BinaryTrees<'a>>::Key>;

View File

@ -12,7 +12,9 @@ pub mod clone_func;
mod controlflow;
pub mod copy_func;
pub mod derivations;
mod extensions;
pub mod instances;
mod shared;
#[cfg(test)]
pub mod test_suite;
#[cfg(test)]
@ -27,8 +29,10 @@ pub use self::applicative_select::{
};
use self::controlflow::{BindableMut, ControlFlowInstance};
pub use self::controlflow::{Iterative, IterativeWrapped};
pub use self::extensions::{MonadExt, MonadFailAnyExt};
#[cfg(doc)]
use self::instances::stackless::StacklessInstance;
pub use self::shared::{SharedFunctor, SharedFunctorAny};
pub trait WeakFunctorAny {
/// Type of the wrapped value.
@ -137,27 +141,6 @@ pub trait Monad<'a>: Applicative<'a> {
}
}
pub trait MonadExt<'a>: Monad<'a> {
/// [`FnMut`] version of [`Monad::iterate`].
/// Reasoning for this method existing at all is that
/// most usecases are better modelled with [`FnMut`]
/// rather than some dedicated state type.
fn iterate_mut<A: 'a, B: 'a>(
a: A,
f: impl 'a + FnMut(A) -> Self::F<ControlFlow<B, A>>,
) -> Self::F<B> {
Self::iterate(BindableMut::new(a, f))
}
fn bind2<A: 'a, B: 'a, C: 'a>(
fa: Self::F<A>,
fb: Self::F<B>,
f: impl 'a + FnOnce(A, B) -> Self::F<C>,
) -> Self::F<C> {
Self::join(Self::la2(fa, fb, f))
}
}
impl<'a, T: Monad<'a>> MonadExt<'a> for T {}
/// Part of [`MonadFail`] responsible for Haskell's `fail`.
@ -255,49 +238,3 @@ pub trait MonadFailAny<'a>: 'a {
}
pub type WrapE<'a, A, E, Fallible> = Wrap<'a, A, <Fallible as MonadFailAny<'a>>::W<E>>;
pub trait MonadFailAnyExt<'a>: MonadFailAny<'a> {
fn pure<E: 'a, A: 'a>(a: A) -> WrapE<'a, A, E, Self> {
<Self::W<E> as Pure>::pure(a)
}
fn fail<E: 'a, A: 'a>(e: E) -> WrapE<'a, A, E, Self> {
<Self::W<E> as Fail<E>>::fail(e)
}
}
impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {}
pub trait SharedFunctorAny: WeakFunctorAny {
type SharedAny<'a, A: 'a + Clone>: 'a + Clone
where
Self: 'a;
fn share<'a, A: 'a + Clone>(fa: Self::FAny<'a, A>) -> Self::SharedAny<'a, A>
where
Self: 'a;
fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::FAny<'a, A>
where
Self: 'a;
}
pub trait SharedFunctor<'a>: WeakFunctor<'a> {
type Shared<A: 'a + Clone>: 'a + Clone;
fn share<A: 'a + Clone>(fa: Self::F<A>) -> Self::Shared<A>;
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::F<A>;
}
impl<'a, T: 'a + SharedFunctorAny> SharedFunctor<'a> for T {
type Shared<A: 'a + Clone> = T::SharedAny<'a, A>;
fn share<A: 'a + Clone>(fa: Self::F<A>) -> Self::Shared<A> {
T::share(fa)
}
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::F<A> {
T::unshare(sa)
}
}

34
src/func/extensions.rs Normal file
View File

@ -0,0 +1,34 @@
use super::*;
pub trait MonadExt<'a>: Monad<'a> {
/// [`FnMut`] version of [`Monad::iterate`].
/// Reasoning for this method existing at all is that
/// most usecases are better modelled with [`FnMut`]
/// rather than some dedicated state type.
fn iterate_mut<A: 'a, B: 'a>(
a: A,
f: impl 'a + FnMut(A) -> Self::F<ControlFlow<B, A>>,
) -> Self::F<B> {
Self::iterate(BindableMut::new(a, f))
}
fn bind2<A: 'a, B: 'a, C: 'a>(
fa: Self::F<A>,
fb: Self::F<B>,
f: impl 'a + FnOnce(A, B) -> Self::F<C>,
) -> Self::F<C> {
Self::join(Self::la2(fa, fb, f))
}
}
pub trait MonadFailAnyExt<'a>: MonadFailAny<'a> {
fn pure<E: 'a, A: 'a>(a: A) -> WrapE<'a, A, E, Self> {
<Self::W<E> as Pure>::pure(a)
}
fn fail<E: 'a, A: 'a>(e: E) -> WrapE<'a, A, E, Self> {
<Self::W<E> as Fail<E>>::fail(e)
}
}
impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {}

35
src/func/shared.rs Normal file
View File

@ -0,0 +1,35 @@
use super::*;
pub trait SharedFunctorAny: WeakFunctorAny {
type SharedAny<'a, A: 'a + Clone>: 'a + Clone
where
Self: 'a;
fn share<'a, A: 'a + Clone>(fa: Self::FAny<'a, A>) -> Self::SharedAny<'a, A>
where
Self: 'a;
fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::FAny<'a, A>
where
Self: 'a;
}
pub trait SharedFunctor<'a>: WeakFunctor<'a> {
type Shared<A: 'a + Clone>: 'a + Clone;
fn share<A: 'a + Clone>(fa: Self::F<A>) -> Self::Shared<A>;
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::F<A>;
}
impl<'a, T: 'a + SharedFunctorAny> SharedFunctor<'a> for T {
type Shared<A: 'a + Clone> = T::SharedAny<'a, A>;
fn share<A: 'a + Clone>(fa: Self::F<A>) -> Self::Shared<A> {
T::share(fa)
}
fn unshare<A: 'a + Clone>(sa: Self::Shared<A>) -> Self::F<A> {
T::unshare(sa)
}
}

View File

@ -16,14 +16,17 @@ use std::{error::Error, rc::Rc};
use crate::func::*;
pub use self::addresses::*;
pub use self::hashing::*;
pub use self::origin::*;
pub use self::point::*;
pub use self::addresses::Addresses;
pub use self::hashing::{Hash, HASH_SIZE, HASH_ZEROS};
pub use self::origin::{OFctr, Origin};
pub use self::point::Point;
pub use self::points::TakesPoints;
pub use self::resolution::*;
pub use self::serialization::*;
pub use self::slice_deserializer::*;
pub use self::resolution::{
Address, HashResolution, HashResolutionResult, Resolution, ResolutionError, ResolutionFailure,
ResolutionResult, Resolver,
};
pub use self::serialization::{Deserializer, DeserializerExt, Serializable, Serializer};
pub use self::slice_deserializer::SliceDeserializer;
/// Basic support for tracing events across the execution.
pub trait Diagnostic<'a, T: Monad<'a>> {

View File

@ -41,12 +41,12 @@ pub trait Deserializer {
}
/// Extension trait for [Deserializer]s.
pub trait ExtDeserializer {
pub trait DeserializerExt {
/// Try to read exactly `N` bytes.
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]>;
}
impl<D: ?Sized + Deserializer> ExtDeserializer for D {
impl<D: ?Sized + Deserializer> DeserializerExt for D {
fn read_n_const<const N: usize>(&mut self) -> Result<[u8; N], &[u8]> {
let slice = self.read_n(N);
match slice.try_into() {

View File

@ -10,10 +10,12 @@ use crate::rcore::*;
use super::*;
pub use self::rendered::*;
pub use self::rendered::{
RenderedAny, RenderedCommon, RenderedLong, RenderedWide, WithLengthAndWidth,
};
use self::trace::*;
pub use self::traceable::*;
pub use self::traced::*;
pub use self::traceable::Traceable;
pub use self::traced::Traced;
/// [`Diagnostic`] for [Traced] objects.
///

View File

@ -11,9 +11,9 @@ impl<A: Display> Display for WithLengthAndWidth<A> {
impl Display for RenderedAny {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RenderedAny::Common(common) => f.write_fmt(format_args!("{}", common)),
RenderedAny::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
RenderedAny::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
Self::Common(common) => f.write_fmt(format_args!("{}", common)),
Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
}
}
}
@ -21,12 +21,10 @@ impl Display for RenderedAny {
impl Display for RenderedCommon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RenderedCommon::Empty => f.write_fmt(format_args!(".")),
RenderedCommon::Resolution => f.write_fmt(format_args!("?")),
RenderedCommon::Event(event) => f.write_fmt(format_args!("{}", event)),
RenderedCommon::Action { name, rendered } => {
f.write_fmt(format_args!("{} @ {}", name, rendered))
}
Self::Empty => f.write_fmt(format_args!(".")),
Self::Resolution => f.write_fmt(format_args!("?")),
Self::Event(event) => f.write_fmt(format_args!("{}", event)),
Self::Action { name, rendered } => f.write_fmt(format_args!("{} @ {}", name, rendered)),
}
}
}
@ -62,8 +60,8 @@ impl Display for RenderVec<&Vec<WithLengthAndWidth<RenderedLong>>> {
impl Display for RenderedLong {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RenderedLong::Common(common) => f.write_fmt(format_args!("{}", common)),
RenderedLong::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
Self::Common(common) => f.write_fmt(format_args!("{}", common)),
Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
}
}
}
@ -71,8 +69,8 @@ impl Display for RenderedLong {
impl Display for RenderedWide {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RenderedWide::Common(common) => f.write_fmt(format_args!("{}", common)),
RenderedWide::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
Self::Common(common) => f.write_fmt(format_args!("{}", common)),
Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))),
}
}
}