Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributive instance for Representable Functor #2775

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/src/main/scala/cats/Representable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ private trait RepresentableBimonad[F[_], R] extends RepresentableMonad[F, R] wit
R.index(fa)(M.empty)
}

private trait RepresentableDistributive[F[_], R] extends Distributive[F] {

def R: Representable.Aux[F, R]

override def distribute[G[_], A, B](ga: G[A])(f: A => F[B])(implicit G: Functor[G]): F[G[B]] =
R.tabulate(r => G.map(ga)(a => R.index(f(a))(r)))

override def map[A, B](fa: F[A])(f: A => B): F[B] = R.F.map(fa)(f)
}

object Representable {
type Aux[F[_], R] = Representable[F] { type Representation = R }

Expand All @@ -119,12 +129,20 @@ object Representable {
}

/**
* Derives a `Bimonad` instance for any `Representable` functor whos representation
* Derives a `Bimonad` instance for any `Representable` functor whose representation
* has a `Monoid` instance.
*/
def bimonad[F[_], R](implicit Rep: Representable.Aux[F, R], Mon: Monoid[R]): Bimonad[F] =
new RepresentableBimonad[F, R] {
override def R: Representable.Aux[F, R] = Rep
override def M: Monoid[R] = Mon
}

/**
* Derives a `Distributive` instance for any `Representable` functor
*/
def distributive[F[_]](implicit Rep: Representable[F]): Distributive[F] =
new RepresentableDistributive[F, Rep.Representation] {
override def R: Aux[F, Rep.Representation] = Rep
}
}
16 changes: 14 additions & 2 deletions tests/src/test/scala/cats/tests/RepresentableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ package cats.tests
import cats.laws.discipline.SemigroupalTests.Isomorphisms
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
import cats.laws.discipline.{BimonadTests, MiniInt, MonadTests, RepresentableTests, SerializableTests}
import cats.laws.discipline.{
BimonadTests,
DistributiveTests,
MiniInt,
MonadTests,
RepresentableTests,
SerializableTests
}
import cats.{Bimonad, Eq, Eval, Id, Representable}
import org.scalacheck.Arbitrary
import cats.data.Kleisli
Expand Down Expand Up @@ -60,14 +67,19 @@ class RepresentableSuite extends CatsSuite {
}

{
//the monadInstance below made a conflict to resolve this one.
// the monadInstance below made a conflict to resolve this one.
// TODO ceedubs is this needed?
implicit val isoFun1: Isomorphisms[MiniInt => ?] = Isomorphisms.invariant[MiniInt => ?]

implicit val monadInstance = Representable.monad[MiniInt => ?]
checkAll("MiniInt => ?", MonadTests[MiniInt => ?].monad[String, String, String])
}

{
implicit val distributiveInstance = Representable.distributive[Pair]
checkAll("Pair[Int]", DistributiveTests[Pair].distributive[Int, Int, Int, Option, MiniInt => ?])
}

// Syntax tests. If it compiles is "passes"
{
// Pair
Expand Down