Search code examples
scalahaskellfunctional-programmingtypeclassscala-cats

Name of Bi - Functor type class with one contravariant and one covariant parameter


I'm looking to see if there is a standard typeclass for a Bi-Functor that has one Contravariant parameter and one Covariant parameter.

punching the signature (c -> a) -> (b -> d) -> f a b -> f c d results in nothing that matches.

Basically in Scala I'm looking to do:

trait CoContraBiFunctor[F[_, _]] {
  def ccmap[A, B, C, D](fab: F[A, B])(f: C => A)(g: B => D): F[C, D]
}

implicit val ccFunction: CoContraBiFunctor[Function1] = new CoContraBiFunctor[Function] {
  override def ccmap[A, B, C, D](fab: Function[A, B])(f: C => A)(g: B => D): Function[C, D] = new Function[C, D] {
    override def apply(c: C): D = g(fab(f(c)))
  }
}

Anyone have an idea? I definitely am not the first person to look for this.


Solution

  • This is called a profunctor! It is a very useful type of bifunctor that shows up all over the place (for example, in the construction of lenses)! In Haskell it is available as Data.Profunctor in the profunctors package. I am not a Scala person but it looks like it is available in cats as well.