Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Last active February 7, 2019 05:17
Show Gist options
  • Save yasuabe/57fcb3df0eb97cb4ee8e635f23f0b2fa to your computer and use it in GitHub Desktop.
Save yasuabe/57fcb3df0eb97cb4ee8e635f23f0b2fa to your computer and use it in GitHub Desktop.
discipline for Prob Monad
package ex01
import cats.Eq
import cats.laws.discipline.MonadTests
import ex01.Prob.Event
import ex01.ProbInstances._
import org.scalacheck.{Gen, _}
import org.scalatest.FunSuite
import org.typelevel.discipline.scalatest.Discipline
class ProbMonadSpec extends FunSuite with Discipline {
implicit def defaultEq[A]: Eq[A] = (x, y) => x == y
implicit def genRational: Gen[Rational] = for {
n <- Gen.choose(-3, 3)
d <- Gen.choose(-3, 3).retryUntil(_ != 0)
} yield Rational.apply(n, d)
implicit def arbRational: Arbitrary[Rational] = Arbitrary(genRational)
implicit def genProb[A](implicit arb: Arbitrary[List[Event[A]]]): Gen[Prob[A]] =
arb.arbitrary.map(Prob(_))
implicit def arbProb[A](implicit arb: Arbitrary[A]): Arbitrary[Prob[A]] = Arbitrary(genProb[A])
implicit def genCoin: Gen[Coin] = Gen.oneOf[Coin](Heads, Tails)
implicit def cogenCoin: Cogen[Coin] = Cogen { _ match {
case Heads => 1L
case Tails => 0L
}}
implicit def arbCoin: Arbitrary[Coin] = Arbitrary(genCoin)
checkAll("Monad[Prob[Coin]]", MonadTests[Prob].monad[Coin, Coin, Coin])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment