4

给定一个Fun类型的单子

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)
}

问题是:人类如何使用学科库来测试这个 Monad 实例是否遵守 Monad Laws?

以下是部分结果,由于编译器找不到隐式 for 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)]
4

1 回答 1

4

通常,您可以将您的实例放入范围并执行此操作(您可以通过导入来稍微清理一下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—I'm just doing .all.check,因为它不需要您使用 ScalaTest 或实例化 a FunSuite。)

于 2016-09-19T16:48:04.183 回答