Skip to content

Commit

Permalink
Merge pull request #636 from ceedubs/eval-laws
Browse files Browse the repository at this point in the history
Check more Eval instance laws
  • Loading branch information
adelbertc committed Nov 15, 2015
2 parents 7f238dc + 7298ecf commit ff359d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import cats.syntax.all._
* Eval instance -- this can defeat the trampolining and lead to stack
* overflows.
*/
sealed abstract class Eval[A] { self =>
sealed abstract class Eval[A] extends Serializable { self =>

/**
* Evaluate the computation and return an A value.
Expand Down Expand Up @@ -290,7 +290,7 @@ private[cats] trait EvalInstances extends EvalInstances0 {
}

implicit def evalGroup[A: Group]: Group[Eval[A]] =
new EvalGroup[A] { val algebra = Group[A] }
new EvalGroup[A] { val algebra: Group[A] = Group[A] }
}

private[cats] trait EvalInstances0 extends EvalInstances1 {
Expand Down
28 changes: 28 additions & 0 deletions tests/src/test/scala/cats/tests/EvalTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package tests
import scala.math.min
import cats.laws.discipline.{BimonadTests, SerializableTests}
import cats.laws.discipline.arbitrary._
import algebra.laws.{GroupLaws, OrderLaws}

class EvalTests extends CatsSuite {

Expand Down Expand Up @@ -91,4 +92,31 @@ class EvalTests extends CatsSuite {
checkAll("Eval[Int]", BimonadTests[Eval].bimonad[Int, Int, Int])
checkAll("Bimonad[Eval]", SerializableTests.serializable(Bimonad[Eval]))

checkAll("Eval[Int]", GroupLaws[Eval[Int]].group)

{
implicit val A = ListWrapper.monoid[Int]
checkAll("Eval[ListWrapper[Int]]", GroupLaws[Eval[ListWrapper[Int]]].monoid)
}

{
implicit val A = ListWrapper.semigroup[Int]
checkAll("Eval[ListWrapper[Int]]", GroupLaws[Eval[ListWrapper[Int]]].semigroup)
}

{
implicit val A = ListWrapper.order[Int]
checkAll("Eval[ListWrapper[Int]]", OrderLaws[Eval[ListWrapper[Int]]].order)
}

{
implicit val A = ListWrapper.partialOrder[Int]
checkAll("Eval[ListWrapper[Int]]", OrderLaws[Eval[ListWrapper[Int]]].partialOrder)
}

{
implicit val A = ListWrapper.eqv[Int]
checkAll("Eval[ListWrapper[Int]]", OrderLaws[Eval[ListWrapper[Int]]].eqv)
}

}
14 changes: 9 additions & 5 deletions tests/src/test/scala/cats/tests/ListWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import org.scalacheck.Arbitrary.arbitrary
final case class ListWrapper[A](list: List[A]) extends AnyVal

object ListWrapper {
def eqv[A : Eq]: Eq[ListWrapper[A]] =
new Eq[ListWrapper[A]] {
def eqv(x: ListWrapper[A], y: ListWrapper[A]): Boolean =
Eq[List[A]].eqv(x.list, y.list)
}
def order[A:Order]: Order[ListWrapper[A]] = Order[List[A]].on[ListWrapper[A]](_.list)

def partialOrder[A:PartialOrder]: PartialOrder[ListWrapper[A]] = PartialOrder[List[A]].on[ListWrapper[A]](_.list)

def eqv[A : Eq]: Eq[ListWrapper[A]] = Eq[List[A]].on[ListWrapper[A]](_.list)

def foldable: Foldable[ListWrapper] =
new Foldable[ListWrapper] {
Expand All @@ -66,6 +66,8 @@ object ListWrapper {
ListWrapper(SemigroupK[List].combine(x.list, y.list))
}

def semigroup[A]: Semigroup[ListWrapper[A]] = semigroupK.algebra[A]

def monadCombine: MonadCombine[ListWrapper] = {
val M = MonadCombine[List]

Expand All @@ -82,6 +84,8 @@ object ListWrapper {
}
}

def monoid[A]: Monoid[ListWrapper[A]] = monadCombine.algebra[A]

implicit def listWrapperArbitrary[A: Arbitrary]: Arbitrary[ListWrapper[A]] =
Arbitrary(arbitrary[List[A]].map(ListWrapper.apply))

Expand Down

0 comments on commit ff359d7

Please sign in to comment.