diff --git a/src/flow/traversible/unbalanced.rs b/src/flow/traversible/unbalanced.rs index b0f67fe..aab785c 100644 --- a/src/flow/traversible/unbalanced.rs +++ b/src/flow/traversible/unbalanced.rs @@ -282,7 +282,7 @@ mod tests { None, None, ); - assert!(traced.a.is_ok()); - // panic!("{:?}", traced.render().to_vec());D + assert!(traced.value.is_ok()); + // panic!("{:?}", traced.render().to_vec()); } } diff --git a/src/func/classes/effect.rs b/src/func/classes/effect.rs index e5f29f3..c23f8a6 100644 --- a/src/func/classes/effect.rs +++ b/src/func/classes/effect.rs @@ -1,11 +1,11 @@ use crate::func::*; pub trait Effect { - fn pure() -> Self; + fn e_pure() -> Self; - fn seq(lhs: Self, rhs: Self) -> Self; + fn e_seq(lhs: Self, rhs: Self) -> Self; - fn after(self, effect: Self) -> Self; + fn e_after(self, effect: Self) -> Self; } #[derive(Clone)] @@ -15,10 +15,10 @@ pub struct WithEffect { } impl WithEffect { - fn after(self, effect: E) -> Self { + fn e_after(self, effect: E) -> Self { WithEffect { value: self.value, - effect: self.effect.after(effect), + effect: self.effect.e_after(effect), } } } @@ -62,7 +62,7 @@ impl Pure for EffectClass { { WithEffect { value: a, - effect: E::pure(), + effect: E::e_pure(), } } } @@ -77,7 +77,7 @@ impl ApplicativeSeq for EffectClass { { WithEffect { value: (ff.value)(fa.value), - effect: E::seq(ff.effect, fa.effect), + effect: E::e_seq(ff.effect, fa.effect), } } } @@ -93,7 +93,7 @@ impl ApplicativeLA2 for EffectClass { { WithEffect { value: f(fa.value, fb.value), - effect: E::seq(fa.effect, fb.effect), + effect: E::e_seq(fa.effect, fb.effect), } } } @@ -105,7 +105,7 @@ impl ApplicativeTuple for EffectClass { { WithEffect { value: (fa.value, fb.value), - effect: E::seq(fa.effect, fb.effect), + effect: E::e_seq(fa.effect, fb.effect), } } } @@ -118,7 +118,7 @@ impl Applicative for EffectClass { drop(fa.value); WithEffect { value: fb.value, - effect: E::seq(fa.effect, fb.effect), + effect: E::e_seq(fa.effect, fb.effect), } } @@ -129,7 +129,7 @@ impl Applicative for EffectClass { drop(fb.value); WithEffect { value: fa.value, - effect: E::seq(fa.effect, fb.effect), + effect: E::e_seq(fa.effect, fb.effect), } } } @@ -142,17 +142,17 @@ impl Monad for EffectClass { where Self: 'a, { - f(fa.value).after(fa.effect) + f(fa.value).e_after(fa.effect) } fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B> where Self: 'a, { - let mut effect = E::pure(); + let mut effect = E::e_pure(); loop { let fa = f.next(); - effect = fa.effect.after(effect); + effect = fa.effect.e_after(effect); match fa.value { ControlFlow::Continue(next_f) => f = next_f, ControlFlow::Break(b) => return WithEffect { value: b, effect }, @@ -165,7 +165,7 @@ impl Monad for EffectClass { Self::F<'a, A>: 'a, Self: 'a, { - ffa.value.after(ffa.effect) + ffa.value.e_after(ffa.effect) } } diff --git a/src/std/collections/stack.rs b/src/std/collections/stack.rs index 0f2e737..2ff17b8 100644 --- a/src/std/collections/stack.rs +++ b/src/std/collections/stack.rs @@ -272,12 +272,12 @@ mod tests { let traced = stack.clone().vec(); assert_eq!(traced.length(), 0); assert_eq!(traced.width(), 0); - assert_eq!(format!("{}", traced.t), "."); + assert_eq!(format!("{}", traced.effect), "."); let stack: T = Rc::new(stack).trace()?; let traced = stack.clone().vec(); assert_eq!(traced.length(), 3); assert_eq!(traced.width(), 1); - assert_eq!(format!("{}", traced.t), "( ? > ? > ? )"); + assert_eq!(format!("{}", traced.effect), "( ? > ? > ? )"); Ok(()) } diff --git a/src/std/tracing.rs b/src/std/tracing.rs index 9d5f4a8..2fd8d61 100644 --- a/src/std/tracing.rs +++ b/src/std/tracing.rs @@ -22,8 +22,21 @@ pub use self::traced::*; pub struct TracedDiagnostic; /// Implementation of [`Monad`] for [Traced] objects. -#[derive(CovariantFunctor)] -pub struct TracedClass; +pub type TracedClass = classes::effect::EffectClass; + +impl classes::effect::Effect for TraceBox { + fn e_pure() -> Self { + TraceBox::pure() + } + + fn e_seq(lhs: Self, rhs: Self) -> Self { + TraceBox::parallel(lhs, rhs) + } + + fn e_after(self, effect: Self) -> Self { + self.after(effect) + } +} trait WithTrace: Sized { fn with_trace(self, t: TraceBox) -> Traced; @@ -31,15 +44,18 @@ trait WithTrace: Sized { impl WithTrace for A { fn with_trace(self, t: TraceBox) -> Traced { - Traced { a: self, t } + Traced { + value: self, + effect: t, + } } } impl Traced { fn wrapped(self, event: &str) -> Self { Traced { - a: self.a, - t: self.t.wrapped(event), + value: self.value, + effect: self.effect.wrapped(event), } } @@ -49,178 +65,24 @@ impl Traced { fn after(self, t: TraceBox) -> Self { Traced { - a: self.a, - t: self.t.after(t), + value: self.value, + effect: self.effect.after(t), } } fn before(self, t: TraceBox) -> Self { Traced { - a: self.a, - t: self.t.before(t), + value: self.value, + effect: self.effect.before(t), } } pub fn length(&self) -> usize { - self.t.length() + self.effect.length() } pub fn width(&self) -> usize { - self.t.width() - } -} - -impl WeakFunctor for TracedClass { - type F<'a, A: 'a> = Traced; -} - -impl Functor for TracedClass { - fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B> - where - Self: 'a, - { - f(fa.a).with_trace(fa.t) - } - - fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B> - where - Self: 'a, - { - b.with_trace(fa.t) - } - - fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()> - where - Self: 'a, - { - ().with_trace(fa.t) - } -} - -impl Pure for TracedClass { - fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> { - a.with_trace(TraceBox::pure()) - } -} - -impl ApplicativeSeq for TracedClass { - 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, - { - (ff.a)(fa.a).with_trace(TraceBox::parallel(ff.t, fa.t)) - } -} - -impl ApplicativeLA2 for TracedClass { - 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, - { - f(fa.a, fb.a).with_trace(TraceBox::parallel(fa.t, fb.t)) - } -} - -impl ApplicativeTuple for TracedClass { - fn tuple<'a, A: 'a, B: 'a>((fa, fb): (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)> - where - Self: 'a, - { - (fa.a, fb.a).with_trace(TraceBox::parallel(fa.t, fb.t)) - } -} - -impl Applicative for TracedClass { - fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> - where - Self: 'a, - { - fb.a.with_trace(TraceBox::parallel(fa.t, fb.t)) - } - - fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A> - where - Self: 'a, - { - fa.a.with_trace(TraceBox::parallel(fa.t, fb.t)) - } -} - -impl Monad for TracedClass { - fn bind<'a, A: 'a, B: 'a>( - fa: Self::F<'a, A>, - f: impl 'a + FnOnce(A) -> Self::F<'a, B>, - ) -> Self::F<'a, B> - where - Self: 'a, - { - f(fa.a).after(fa.t) - } - - fn iterate_mut<'a, A: 'a, B: 'a>( - mut a: A, - mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow>, - ) -> Self::F<'a, B> - where - Self: 'a, - { - let mut t = TraceBox::pure(); - loop { - let fa = f(a); - t = fa.t.after(t); - match fa.a { - ControlFlow::Continue(next_a) => a = next_a, - ControlFlow::Break(b) => return b.with_trace(t), - } - } - } - - fn iterate_argument<'a, A: 'a, B: 'a>( - mut a: A, - mut f: impl AIterative<'a, T = Self, A = A, B = B>, - ) -> Self::F<'a, B> - where - Self: 'a, - { - let mut t = TraceBox::pure(); - loop { - let fa = f.next(a); - t = fa.t.after(t); - match fa.a { - ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f), - ControlFlow::Break(b) => return b.with_trace(t), - } - } - } - - fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B> - where - Self: 'a, - { - let mut t = TraceBox::pure(); - loop { - let fa = f.next(); - t = fa.t.after(t); - match fa.a { - ControlFlow::Continue(next_f) => f = next_f, - ControlFlow::Break(b) => return b.with_trace(t), - } - } - } - - fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> - where - Self::F<'a, A>: 'a, - Self: 'a, - { - ffa.a.after(ffa.t) + self.effect.width() } } diff --git a/src/std/tracing/trace/render.rs b/src/std/tracing/trace/render.rs index 1767f16..190d2de 100644 --- a/src/std/tracing/trace/render.rs +++ b/src/std/tracing/trace/render.rs @@ -40,7 +40,7 @@ impl Trace { impl Traced { pub fn render(&self) -> WithLengthAndWidth { - self.t.trace.render() + self.effect.trace.render() } } diff --git a/src/std/tracing/traced.rs b/src/std/tracing/traced.rs index ecb4b5d..45d8511 100644 --- a/src/std/tracing/traced.rs +++ b/src/std/tracing/traced.rs @@ -3,7 +3,4 @@ use super::*; /// Wrapper containing the value and the corresponding execution trace. /// /// For what the trace contains, see its rendered form, [`RenderedAny`]. -pub struct Traced { - pub a: A, - pub t: TraceBox, -} +pub type Traced = classes::effect::WithEffect; diff --git a/src/testing/counted.rs b/src/testing/counted.rs index f718c25..0a9c86f 100644 --- a/src/testing/counted.rs +++ b/src/testing/counted.rs @@ -25,15 +25,15 @@ impl Context for TestContextCounted { pub type CountedClass = classes::effect::EffectClass; impl classes::effect::Effect for usize { - fn pure() -> Self { + fn e_pure() -> Self { 0 } - fn seq(lhs: Self, rhs: Self) -> Self { + fn e_seq(lhs: Self, rhs: Self) -> Self { max(lhs, rhs) } - fn after(self, effect: Self) -> Self { + fn e_after(self, effect: Self) -> Self { self + effect } }