如何使用纪律测试 Monad 实例

How to test Monad instance using discipline

给定一个 Fun 类型的 monad

type FUN[A] = Map[String, String] => (List[String], A)

val funMonad: Monad[FUN] = new Monad[FUN] {
  override def flatMap[A, B](fa: FUN[A])(f: (A) => FUN[B]): FUN[B] = m => {
    val (list1, a1) = fa(m)
    val (list2, a2) = f(a1)(m)
    (list1 ++ list2, a2)
  }
  override def pure[A](x: A): FUN[A] = m => (Nil, x)
}

问题是:如何使用 discipline library 来测试这个 Monad 实例是否遵守 Monad 法则?

下面是部分结果,它失败了,因为编译器找不到隐含的 CartesianTests.Isomorphisms[FUN].

import cats.Monad
import cats.kernel.Eq
import org.scalacheck.rng.Seed
import org.scalacheck.{Arbitrary, Gen}

class MyMonadSpec extends FunSuite with scalatest.Discipline {

...

implicit def funEq[T: Eq]: Eq[FUN[T]] = {
  val sampleInput: Map[String, String] = {
    def genMap: Gen[Map[String, String]] = for {
      size <- Gen.size
      keys <- Gen.containerOfN[List, String](size, Arbitrary.arbitrary[String])
      values <- Gen.containerOfN[List, String](size, Arbitrary.arbitrary[String])
    } yield keys.zip(values).toMap

    genMap(Gen.Parameters.default.withSize(10), Seed.apply(123L)).get
  }

  Eq.instance[FUN[T]] ((f1, f2) => f1(sampleInput) == f2(sampleInput))
}

import cats.kernel.instances.int._
import cats.kernel.instances.tuple._
import cats.laws.discipline.MonadTests


checkAll("Int", MonadTests[FUN](funMonad).monad[Int, Int, Int])

//Error: could not find implicit value for parameter iso: cats.laws.discipline.CartesianTests.Isomorphisms[[A]scala.collection.immutable.Map[String,String] => (List[String], A)]

通常你可以将你的实例放入范围并执行此操作(你可以通过导入 cats.instances.all._ 稍微清理一下,但为了清楚起见,我要明确说明):

import cats.instances.int._
import cats.instances.list._
import cats.instances.map._
import cats.instances.string._
import cats.instances.tuple._
import cats.laws.discipline.MonadTests
import cats.laws.discipline.eq._

MonadTests[FUN].monad[Int, Int, Int].all.check

您将免费获得 Isomorphisms 实例,因为它只需要一个 Invariant 实例,这由 Monad 暗示。另请注意,您不需要定义自己的 Eq 实例 - eq 包提供了一个适合您测试的 Function1 实例。

在这种情况下,编译器实际上不会找到 Invariant 实例(这可能是 SI-2712 问题,也可能与别名有关——超出了我的想象)我不确定),而且您似乎想测试 monad 实例而不将其放入隐式范围。一种简单的方法是提供您自己的 Isomorphisms:

import cats.laws.discipline.CartesianTests.Isomorphisms

implicit val funIsomorphisms: Isomorphisms[FUN] = Isomorphisms.invariant(funMonad)

或作为一个完整的工作示例(在 Cats 0.7.2 上):

import cats.Monad
import cats.instances.int._
import cats.instances.list._
import cats.instances.map._
import cats.instances.string._
import cats.instances.tuple._
import cats.instances.map._
import cats.laws.discipline.CartesianTests.Isomorphisms
import cats.laws.discipline.MonadTests
import cats.laws.discipline.eq._

type FUN[A] = Map[String, String] => (List[String], A)

val funMonad: Monad[FUN] = new Monad[FUN] {
  def flatMap[A, B](fa: FUN[A])(f: (A) => FUN[B]): FUN[B] = m => {
    val (list1, a1) = fa(m)
    val (list2, a2) = f(a1)(m)
    (list1 ++ list2, a2)
  }
  def pure[A](x: A): FUN[A] = m => (Nil, x)
  def tailRecM[A, B](a: A)(f: A => FUN[Either[A, B]]): FUN[B] = defaultTailRecM(a)(f)
}

implicit val funIsomorphisms: Isomorphisms[FUN] = Isomorphisms.invariant(funMonad)

然后:

scala> MonadTests[FUN](funMonad).monad[Int, Int, Int].all.check
+ monad.ap consistent with product + map: OK, passed 100 tests.
+ monad.applicative homomorphism: OK, passed 100 tests.
+ monad.applicative identity: OK, passed 100 tests.
+ monad.applicative interchange: OK, passed 100 tests.
+ monad.applicative map: OK, passed 100 tests.
+ monad.apply composition: OK, passed 100 tests.
+ monad.cartesian associativity: OK, passed 100 tests.
+ monad.covariant composition: OK, passed 100 tests.
+ monad.covariant identity: OK, passed 100 tests.
+ monad.flatMap associativity: OK, passed 100 tests.
+ monad.flatMap consistent apply: OK, passed 100 tests.
+ monad.followedBy consistent flatMap: OK, passed 100 tests.
+ monad.invariant composition: OK, passed 100 tests.
+ monad.invariant identity: OK, passed 100 tests.
+ monad.map flatMap coherence: OK, passed 100 tests.
+ monad.monad left identity: OK, passed 100 tests.
+ monad.monad right identity: OK, passed 100 tests.
+ monad.monoidal left identity: OK, passed 100 tests.
+ monad.monoidal right identity: OK, passed 100 tests.
+ monad.mproduct consistent flatMap: OK, passed 100 tests.
+ monad.tailRecM consistent flatMap: OK, passed 100 tests.

(您也可以使用 checkAll——我只是在做 .all.check 因为它不需要您拥有 ScalaTest 或实例化一个 FunSuite。)