Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用语义等价的cats功能替换scalaz ListT_List_Scala_Scalaz_Monad Transformers_Scala Cats - Fatal编程技术网

用语义等价的cats功能替换scalaz ListT

用语义等价的cats功能替换scalaz ListT,list,scala,scalaz,monad-transformers,scala-cats,List,Scala,Scalaz,Monad Transformers,Scala Cats,cats确实提供了ListTmonad transformer,那么我们如何重写下面使用scalazListT来理解cats中语义等价的片段呢 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]]

cats确实提供了
ListT
monad transformer,那么我们如何重写下面使用scalaz
ListT
来理解cats中语义等价的片段呢

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

这种重构似乎满足了typechecker的要求,但我不确定happy compiler是否能确保100%的语义等价。

Cats不提供ListT,因为它违反了关联性单子定律。见和

不过,下面的
ListT
实现基于
.flatTraverse
,正如您所建议的那样,通过了所有cats核心法测试(一个bug?)

我没有软件验证的经验,但你可能会发现成功的测试足以将这2个实现看作是等价的。 ListT实现
case class ListT[M[_],A](值:M[List[A]]
隐式def listTMonad[M[\u]:Monad]=新的Monad[ListT[M,*]]{
覆盖def flatMap[A,B](fa:ListT[M,A])(f:A=>ListT[M,B]):ListT[M,B]=
利斯特(
Monad[M].flatMap[List[A],List[B]](fa.value)(
列表=>遍历[list]。平面遍历[M,A,B](列表)(A=>f(A).value)
)
)
重写def pure[A](A:A):ListT[M,A]=ListT(Monad[M].pure(List(A)))
//对于此问题,可以忽略不安全impl
覆盖def tailRecM[A,B](A:A)(f:A=>ListT[M,或[A,B]]):ListT[M,B]=
平面图(f(a)){
案例右侧(b)=>纯(b)
案例左侧(nextA)=>tailRecM(nextA)(f)
}
}
sbt
name:=“列表测试”
版本:=“0.1”
规模厌恶:=“2.11.12”
scalacOptions+=“-部分统一”
libraryDependencies++=Seq(
“org.typelevel”%%“cats核心”%%“2.0.0”,
“org.scalaz”%%“scalaz核心”%%“7.2.30”,
“org.scalacheck”%%“scalacheck”%%“1.14.1”%%“测试”,
“org.scalatest”%%“scalatest”%%“2.2.6”%%“测试”,
“org.typelevel”%%“规程等级测试”%%“1.0.1”,
“组织类型级别”%%“学科核心”%%“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”%%“种类投影仪”%%“0.11.0”cross CrossVersion.full)
法律考试
class TreeLawTests使用带有FunSpec规则的检查器扩展AnyFunSpec{
隐式定义ListEq[M[]A]=Eq.fromUniversalEquals[ListT[M,A]]
checkAll(“ListT Monad Laws”,MonadTests[ListT[Option,*]]。stackUnsafeMonad[Int,Int,String])
}
法律测试结果
考虑关联性法律不成立的相关问题:
import cats.implicits._

seeds
  .flatMap {
    _.flatTraverse { seed =>
      grow(seed)
        .flatMap {
          _.flatTraverse { plant =>
            family(seed, plant)
          }
        }
    }
  }
- 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