PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/scalacheck-binding/src/main/scala/scalaz/scalacheck/ScalaCheckBinding.scala

http://github.com/scalaz/scalaz
Scala | 42 lines | 32 code | 6 blank | 4 comment | 0 complexity | d440e4b4320ca5ebc04accbeda7f09d7 MD5 | raw file
 1package scalaz
 2package scalacheck
 3
 4/**
 5 * Type class instances for types from [[https://github.com/rickynils/scalacheck Scalacheck]]
 6 */
 7object ScalaCheckBinding {
 8  import org.scalacheck.{Cogen, Gen, Arbitrary, Shrink}
 9  import Gen.{sized, const}
10
11  implicit val ArbitraryMonad: Monad[Arbitrary] = new Monad[Arbitrary] {
12    def bind[A, B](fa: Arbitrary[A])(f: A => Arbitrary[B]) = Arbitrary(fa.arbitrary.flatMap(f(_).arbitrary))
13    def point[A](a: => A) = Arbitrary(sized(_ => const(a)))
14    override def map[A, B](fa: Arbitrary[A])(f: A => B) = Arbitrary(fa.arbitrary.map(f))
15  }
16
17  implicit val GenMonad: Monad[Gen] = new Monad[Gen] {
18    def point[A](a: => A) = sized(_ => const(a))
19    def bind[A, B](fa: Gen[A])(f: A => Gen[B]) = fa flatMap f
20    override def map[A, B](fa: Gen[A])(f: A => B) = fa map f
21  }
22
23  implicit val CogenInstance: Divisible[Cogen] = new Divisible[Cogen] {
24    override def contramap[A, B](a: Cogen[A])(f: B => A) =
25      a contramap f
26    override def conquer[A] =
27      Cogen((seed, _) => seed)
28    override def divide2[A, B, C](fa: =>Cogen[A], fb: =>Cogen[B])(f: C => (A, B)) =
29      Cogen{ (seed, c) =>
30        val (a, b) = f(c)
31        fb.perturb(fa.perturb(seed, a), b)
32      }
33  }
34
35  implicit val ShrinkFunctor: InvariantFunctor[Shrink] =
36    new InvariantFunctor[Shrink] {
37      def xmap[A, B](ma: Shrink[A], f: A => B, g: B => A): Shrink[B] =
38        Shrink{b => ma shrink g(b) map f}
39    }
40}
41
42// vim: expandtab:ts=2:sw=2