Skip to main content

karpal_profunctor/
profunctor.rs

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