11

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.

Nigel Benns
  • 1,236
  • 11
  • 14
  • 1
    Where did you "punch the signature"? [I found it that way](https://hoogle.haskell.org/?hoogle=(c%20-%3E%20a)%20-%3E%20(b%20-%3E%20d)%20-%3E%20f%20a%20b%20-%3E%20f%20c%20d). Fun fact, there are [three](http://haskell.org/hoogle) [different](https://hoogle.haskell.org) [hoogles](https://www.stackage.org/package/hoogle) and they all give different results! Only `hoogle.haskell.org` gives a result for your query. :-/ – luqui Feb 14 '19 at 04:39

1 Answers1

12

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.

Rein Henrichs
  • 15,437
  • 1
  • 45
  • 55
  • Thank you I knew I had heard it before and I just couldn't remember – Nigel Benns Feb 14 '19 at 02:54
  • 1
    Re profuctorial lenses: Some useful links are http://oleg.fi/gists/posts/2017-04-18-glassery.html (for pure profunctor lenses) as well as https://artyom.me/lens-over-tea-4 and https://artyom.me/lens-over-tea-5 (for van Laarhoven lenses). The diagrams in the last one in particular is what made profunctors 'click' for me, but they're all fascinating reads. – bradrn Feb 14 '19 at 04:50