typelevel / cats

Lightweight, modular, and extensible library for functional programming.
https://typelevel.org/cats/
Other
5.26k stars 1.21k forks source link

Add Choice Profunctor #1175

Open tel opened 8 years ago

tel commented 8 years ago

Useful for bootstrapping pure profunctor lenses

trait Choice[~>[_, _]] extends Profunctor[~>] {
  def left[A, B, C](p: A ~> B): (C Xor A) ~> (C Xor B)
  def right[A, B, C](p: A ~> B): (A Xor C) ~> (B Xor C)
}

object Choice {
  def apply[F[_, _]](implicit ev: Choice[F]): Choice[F] = ev
}
fthomas commented 8 years ago

There is an open PR (#324) by @julien-truffaut that adds this type class under the name ProChoice. AFAIC it just needs a bit polishing/documentation.

tel commented 8 years ago

Yep! Thanks for pointing it out. Is there a reason it's ProChoice instead of just Choice, though? Not to bikeshed, but I'm new to the project and that seems very odd to me.

fthomas commented 8 years ago

One reason for not calling it Choice is probably because there is already a different Choice type class.

tel commented 8 years ago

At a high level that seems sort of backwards, though. Arrow Choice should just be Profunctor Choice to begin with. The only extra thing you get is the codiagonal bit.

tel commented 8 years ago

Maybe I should change this PR to "Generalize arrow.Choice"?

tel commented 8 years ago

Also, fwiw, here's a use case I'm developing

https://github.com/tel/scala-telescope/blob/master/src/main/scala-2.11/jspha/telescope/Prism.scala

LukaJCB commented 6 years ago

This can be closed now that we have ArrowChoice :)

tel commented 6 years ago

I disagree! The Choice in cats is still too strong. This stuff needs to be defined below Category. A Choice profunctor which doesn’t admit an identity is a real thing. For instance, Fold[A, B] which consumes any number of As and emits on B is a choice profunctor without being a category.