Замените скаляз ListT семантически эквивалентной функциональностью кошек - PullRequest
3 голосов
/ 16 февраля 2020

cats не предоставляет ListT монадный преобразователь, так как мы могли бы переписать следующий фрагмент , который использует скалаз ListT для понимания семантически эквивалентного фрагмента у кошек

import scalaz._
import ListT._
import scalaz.std.option._

val seeds: Option[List[String]] = Some(List("apple", "orange", "tomato"))
def grow(seed: String): Option[List[String]] = Some(List(seed.toUpperCase))
def family(seed: String, plant: String): Option[List[(String, String)]] = Some(List(seed -> plant))

(for {
  seed    <- listT(seeds)
  plant   <- listT(grow(seed))
  result  <- listT(family(seed, plant))
} yield result).run

Вот моя попытка использовать flatMap и flatTraverse

import cats.implicits._

seeds
  .flatMap {
    _.flatTraverse { seed =>
      grow(seed)
        .flatMap {
          _.flatTraverse { plant =>
            family(seed, plant)
          }
        }
    }
  }

Этот рефакторинг удовлетворяет проверку типов, однако я не уверен, что счастливый компилятор обеспечивает 100% эквивалентность semanti c.

1 Ответ

1 голос
/ 26 февраля 2020

Кошки не предоставляют ListT, потому что это нарушает закон монады ассоциативности. См. FAQ по кошкам и связанное доказательство с использованием скаляза ListT .

Тем не менее следующая реализация ListT на основе .flatTraverse, как вы предлагаете, проходит все основные тесты закона о кошках (Жук?).

У меня нет опыта в проверке программного обеспечения, но вы можете найти успешные тесты достаточно хорошими, чтобы рассматривать 2 реализации как эквивалентные.

Реализация ListT

case class ListT[M[_], A](value: M[List[A]])
implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] {
  override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] =
    ListT(
      Monad[M].flatMap[List[A], List[B]](fa.value)(
        list => Traverse[List].flatTraverse[M, A, B](list)(a => f(a).value)
      )
    )
  override def pure[A](a: A): ListT[M, A] = ListT(Monad[M].pure(List(a)))
  // unsafe impl, can be ignored for this question
  override def tailRecM[A, B](a: A)(f: A => ListT[M, Either[A, B]]): ListT[M, B] =
    flatMap(f(a)) {
      case Right(b) => pure(b)
      case Left(nextA) => tailRecM(nextA)(f)
    }
}

sbt

name := "listT_tests"
version := "0.1"
scalaVersion := "2.11.12"

scalacOptions += "-Ypartial-unification"

libraryDependencies ++= Seq(
  "org.typelevel" %% "cats-core" % "2.0.0",
  "org.scalaz" %% "scalaz-core" % "7.2.30",
  "org.scalacheck" %% "scalacheck" % "1.14.1" % "test",
  "org.scalatest" %% "scalatest" % "2.2.6" % "test",
  "org.typelevel" %% "discipline-scalatest" % "1.0.1",
  "org.typelevel" %% "discipline-core" % "1.0.2",
  "org.typelevel" %% "cats-laws" % "2.0.0" % Test,
  "com.github.alexarchambault" %% "scalacheck-shapeless_1.14" % "1.2.3" % Test
)

addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.11.0" cross CrossVersion.full)

юридические тесты

class TreeLawTests extends AnyFunSpec with Checkers with FunSpecDiscipline {

  implicit def listTEq[M[_], A] = Eq.fromUniversalEquals[ListT[M, A]]
  checkAll("ListT Monad Laws", MonadTests[ListT[Option, *]].stackUnsafeMonad[Int, Int, String])
}

результаты юридических тестов

- monad (stack-unsafe).ap consistent with product + map
- monad (stack-unsafe).applicative homomorphism
- monad (stack-unsafe).applicative identity
- monad (stack-unsafe).applicative interchange
- monad (stack-unsafe).applicative map
- monad (stack-unsafe).applicative unit
- monad (stack-unsafe).apply composition
- monad (stack-unsafe).covariant composition
- monad (stack-unsafe).covariant identity
- monad (stack-unsafe).flatMap associativity
- monad (stack-unsafe).flatMap consistent apply
- monad (stack-unsafe).flatMap from tailRecM consistency
- monad (stack-unsafe).invariant composition
- monad (stack-unsafe).invariant identity
- monad (stack-unsafe).map flatMap coherence
- monad (stack-unsafe).map2/map2Eval consistency
- monad (stack-unsafe).map2/product-map consistency
- monad (stack-unsafe).monad left identity
- monad (stack-unsafe).monad right identity
- monad (stack-unsafe).monoidal left identity
- monad (stack-unsafe).monoidal right identity
- monad (stack-unsafe).mproduct consistent flatMap
- monad (stack-unsafe).productL consistent map2
- monad (stack-unsafe).productR consistent map2
- monad (stack-unsafe).semigroupal associativity
- monad (stack-unsafe).tailRecM consistent flatMap
...