Skip to main content

Profunctor

Trait Profunctor 

Source
pub trait Profunctor: Kind_266801a817966495 {
    // Required method
    fn dimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
        ab: impl Fn(A) -> B + 'a,
        cd: impl Fn(C) -> D + 'a,
        pbc: <Self as Kind_266801a817966495>::Of<'a, B, C>,
    ) -> <Self as Kind_266801a817966495>::Of<'a, A, D>;

    // Provided methods
    fn map_input<'a, A: 'a, B: 'a, C: 'a>(
        ab: impl Fn(A) -> B + 'a,
        pbc: <Self as Kind_266801a817966495>::Of<'a, B, C>,
    ) -> <Self as Kind_266801a817966495>::Of<'a, A, C> { ... }
    fn map_output<'a, A: 'a, B: 'a, C: 'a>(
        bc: impl Fn(B) -> C + 'a,
        pab: <Self as Kind_266801a817966495>::Of<'a, A, B>,
    ) -> <Self as Kind_266801a817966495>::Of<'a, A, C> { ... }
}
Expand description

A type class for profunctors.

A profunctor is a type constructor that is contravariant in its first type parameter and covariant in its second type parameter. This means it can pre-compose with a function on the input and post-compose with a function on the output.

§Hierarchy Unification

This trait is the root of the unified profunctor and arrow hierarchies on Kind!(type Of<'a, A: 'a, B: 'a>: 'a;). This unification ensures that all profunctor-based abstractions (including lenses and prisms) share a consistent higher-kinded representation with strict lifetime bounds.

By explicitly requiring that both type parameters outlive the application lifetime 'a, we provide the compiler with the necessary guarantees to handle trait objects (like dyn Fn) commonly used in profunctor implementations. This resolves potential E0310 errors where the compiler cannot otherwise prove that captured variables in closures satisfy the required lifetime bounds.

§Laws

Profunctor instances must satisfy the following laws:

  • Identity: dimap(identity, identity, p) = p.
  • Composition: dimap(f2 ∘ f1, g1 ∘ g2, p) = dimap(f1, g1, dimap(f2, g2, p)).

§Examples

Profunctor laws for RcFnBrand:

use fp_library::{
	brands::*,
	functions::*,
};

let p = std::rc::Rc::new(|x: i32| x + 1) as std::rc::Rc<dyn Fn(i32) -> i32>;

// Identity: dimap(identity, identity, p) = p
let id_mapped = dimap::<RcFnBrand, _, _, _, _>(identity, identity, p.clone());
assert_eq!(id_mapped(5), p(5));
assert_eq!(id_mapped(0), p(0));

// Composition: dimap(f2 ∘ f1, g1 ∘ g2, p)
//            = dimap(f1, g1, dimap(f2, g2, p))
let f1 = |x: i32| x + 10;
let f2 = |x: i32| x * 2;
let g1 = |x: i32| x - 1;
let g2 = |x: i32| x * 3;
let left = dimap::<RcFnBrand, _, _, _, _>(
	compose(f2, f1), // f2 ∘ f1
	compose(g1, g2), // g1 ∘ g2
	p.clone(),
);
let right = dimap::<RcFnBrand, _, _, _, _>(f1, g1, dimap::<RcFnBrand, _, _, _, _>(f2, g2, p));
assert_eq!(left(5), right(5));
assert_eq!(left(0), right(0));

Required Methods§

Source

fn dimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>( ab: impl Fn(A) -> B + 'a, cd: impl Fn(C) -> D + 'a, pbc: <Self as Kind_266801a817966495>::Of<'a, B, C>, ) -> <Self as Kind_266801a817966495>::Of<'a, A, D>

Maps over both arguments of the profunctor.

This method applies a contravariant function to the first argument and a covariant function to the second argument, transforming the profunctor.

§Type Signature

forall A B C D. (A -> B, C -> D, Self B C) -> Self A D

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The new input type (contravariant position).
  • B: The original input type.
  • C: The original output type.
  • D: The new output type (covariant position).
§Parameters
  • ab: The contravariant function to apply to the input.
  • cd: The covariant function to apply to the output.
  • pbc: The profunctor instance.
§Returns

A new profunctor instance with transformed input and output types.

§Examples
use fp_library::{
	brands::*,
	classes::profunctor::*,
	functions::*,
};

let f = |x: i32| x + 1;
let g = dimap::<RcFnBrand, _, _, _, _>(
	|x: i32| x * 2,
	|x: i32| x - 1,
	std::rc::Rc::new(f) as std::rc::Rc<dyn Fn(i32) -> i32>,
);
assert_eq!(g(10), 20); // (10 * 2) + 1 - 1 = 20

Provided Methods§

Source

fn map_input<'a, A: 'a, B: 'a, C: 'a>( ab: impl Fn(A) -> B + 'a, pbc: <Self as Kind_266801a817966495>::Of<'a, B, C>, ) -> <Self as Kind_266801a817966495>::Of<'a, A, C>

Maps contravariantly over the first argument.

This is a convenience method that maps only over the input (contravariant position). Corresponds to lmap in Haskell and lcmap in PureScript.

§Type Signature

forall A B C. (A -> B, Self B C) -> Self A C

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The new input type.
  • B: The original input type.
  • C: The output type.
§Parameters
  • ab: The contravariant function to apply to the input.
  • pbc: The profunctor instance.
§Returns

A new profunctor instance with transformed input type.

§Examples
use fp_library::{
	brands::*,
	classes::profunctor::*,
	functions::*,
};

let f = |x: i32| x + 1;
let g = map_input::<RcFnBrand, _, _, _>(
	|x: i32| x * 2,
	std::rc::Rc::new(f) as std::rc::Rc<dyn Fn(i32) -> i32>,
);
assert_eq!(g(10), 21); // (10 * 2) + 1 = 21
Source

fn map_output<'a, A: 'a, B: 'a, C: 'a>( bc: impl Fn(B) -> C + 'a, pab: <Self as Kind_266801a817966495>::Of<'a, A, B>, ) -> <Self as Kind_266801a817966495>::Of<'a, A, C>

Maps covariantly over the second argument.

This is a convenience method that maps only over the output (covariant position). Corresponds to rmap in both Haskell and PureScript.

§Type Signature

forall A B C. (B -> C, Self A B) -> Self A C

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The input type.
  • B: The original output type.
  • C: The new output type.
§Parameters
  • bc: The covariant function to apply to the output.
  • pab: The profunctor instance.
§Returns

A new profunctor instance with transformed output type.

§Examples
use fp_library::{
	brands::*,
	classes::profunctor::*,
	functions::*,
};

let f = |x: i32| x + 1;
let g = map_output::<RcFnBrand, _, _, _>(
	|x: i32| x * 2,
	std::rc::Rc::new(f) as std::rc::Rc<dyn Fn(i32) -> i32>,
);
assert_eq!(g(10), 22); // (10 + 1) * 2 = 22

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Profunctor for TaggedBrand

Source§

impl<FunctionBrand: LiftFn + 'static> Profunctor for ZippingBrand<FunctionBrand>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for BazaarBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The focus type.
  • B: The replacement type.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for ExchangeBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The type of the value produced by the forward function.
  • B: The type of the value consumed by the backward function.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for GratingBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The type of the value produced by the inner function.
  • B: The type of the value consumed by the inner function.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for MarketBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The type of the value produced by the preview function.
  • B: The type of the value consumed by the review function.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for ShopBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The type of the value produced by the getter.
  • B: The type of the value consumed by the setter.
Source§

impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor for StallBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The type of the value produced by the preview function.
  • B: The type of the value consumed by the setter.
Source§

impl<InnerP: Profunctor + 'static, PointerBrand: ToDynCloneFn + 'static, S: 'static, T: 'static> Profunctor for ReverseBrand<InnerP, PointerBrand, S, T>

Profunctor instance for ReverseBrand<InnerP, OuterP, S, T> whenever InnerP: Profunctor.

Corresponds to:

instance profunctorRe :: Profunctor p => Profunctor (Re p s t) where
  dimap f g (Re r) = Re (r <<< dimap g f)
§Type Parameters
  • InnerP: The inner profunctor brand.
  • PointerBrand: The outer cloneable function pointer brand.
  • S: The fixed source type.
  • T: The fixed target type.
Source§

impl<P: ToDynCloneFn> Profunctor for FnBrand<P>

§Type Parameters
  • P: The reference-counted pointer type.
Source§

impl<P: Profunctor + 'static, I: 'static> Profunctor for IndexedBrand<P, I>

§Type Parameters
  • P: The underlying profunctor brand.
  • I: The index type.
Source§

impl<PointerBrand: ToDynCloneFn + 'static, R: 'static> Profunctor for ForgetBrand<PointerBrand, R>

§Type Parameters
  • PointerBrand: The pointer brand.
  • R: The return type of the function.