Skip to main content

karpal_profunctor/
profunctor.rs

1pub use karpal_core::hkt::HKT2;
2
3/// A profunctor is contravariant in its first argument and covariant in its second.
4///
5/// Laws:
6/// - Identity: `dimap(id, id, p) == p`
7/// - Composition: `dimap(f . g, h . i, p) == dimap(g, h, dimap(f, i, p))`
8pub trait Profunctor: HKT2 {
9    fn dimap<A: 'static, B: 'static, C, D>(
10        f: impl Fn(C) -> A + 'static,
11        g: impl Fn(B) -> D + 'static,
12        pab: Self::P<A, B>,
13    ) -> Self::P<C, D>;
14
15    fn lmap<A: 'static, B: 'static, C>(
16        f: impl Fn(C) -> A + 'static,
17        pab: Self::P<A, B>,
18    ) -> Self::P<C, B> {
19        Self::dimap(f, |b| b, pab)
20    }
21
22    fn rmap<A: 'static, B: 'static, D>(
23        g: impl Fn(B) -> D + 'static,
24        pab: Self::P<A, B>,
25    ) -> Self::P<A, D> {
26        Self::dimap(|a| a, g, pab)
27    }
28}