gpt4 book ai didi

scala - 在scalaz中堆叠StateT

转载 作者:行者123 更新时间:2023-12-04 02:09:51 25 4
gpt4 key购买 nike

我试图通过移植 Dan Piponi 的本教程中的一些示例来理解 Scala 中的 Monad Transformers:http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html

我做了几个简单的:

import Control.Monad.State
import Control.Monad.Identity

test1 = do
a <- get
modify (+1)
b <- get
return (a,b)

test2 = do
a <- get
modify (++"1")
b <- get
return (a,b)

go1 = evalState test1 0
go2 = evalState test2 "0"

变成:

import scalaz._, Scalaz._

val test1 = for {
a <- get[Int]
_ <- modify[Int](1+)
b <- get
} yield (a,b)

val test2 = for {
a <- get[String]
_ <- modify[String](_ + "1")
b <- get
} yield (a,b)

val go1 = test1.eval(0)
val go2 = test2.eval("0")

但是我怎么能把下一个例子移植到 Scala 中呢?

test3 = do
modify (+ 1)
lift $ modify (++ "1")
a <- get
b <- lift get
return (a,b)

go3 = runIdentity $ evalStateT (evalStateT test3 0) "0"

我已经使用 scalaz 7.1.0-M6 做到了这一点:

type SST[F[_],A] = StateT[F,String,A]
type IST[F[_],A] = StateT[F,Int,A]

val p1: StateT[Id,Int,Unit] = modify[Int](1+)
val p2: StateT[Id,String,Unit] = modify[String](_ + "1")
val p3: StateT[({type l[a]=StateT[Id,String,a]})#l,Int,Unit] = p2.liftM[IST]

但这还没有接近,据我所知甚至可能倒退。

我当然可以这样做:
import scalaz.Lens._
val test3 = for {
_ <- firstLens[Int,String] lifts (modify (1+))
_ <- secondLens[Int,String] lifts (modify (_ + "1"))
a <- firstLens[Int,String] lifts get
b <- secondLens[Int,String] lifts get
} yield (a,b)

val go3 = test3.eval(0,"0")

但是我根本没有使用stacked StateT,所以它没有回答这个问题。

提前致谢!

最佳答案

问题是 modify你得到的通常进口来自State ,并且不会帮助您处理 StateT .

从 Haskell 类型签名开始是个好主意:

test3
:: (MonadState [Char] m, MonadState s (t m), MonadTrans t,
Num s) =>
t m (s, [Char])

您应该能够将其翻译成如下内容:
import scalaz._, Scalaz._

def test3[M[_]: Monad](implicit
inner: MonadState[({ type T[s, a] = StateT[M, s, a] })#T, String],
outer: MonadState[
({
type T[s, a] = StateT[({ type L[y] = StateT[M, String, y] })#L, s, a ]
})#T,
Int
],
mt: MonadTrans[({ type L[f[_], a] = StateT[f, Int, a] })#L]
) = for {
_ <- outer.modify(_ + 1)
_ <- mt.liftMU(inner.modify(_ + "1"))
a <- outer.get
b <- mt.liftMU(inner.get)
} yield (a, b)

这很可怕,但它是对 Haskell 的相当直接的改写。由于某种原因,编译器似乎找不到 outer但是,例如,您必须提供一些帮助:
def test3[M[_]: Monad](implicit
inner: MonadState[({ type T[s, a] = StateT[M, s, a] })#T, String],
mt: MonadTrans[({ type L[f[_], a] = StateT[f, Int, a] })#L]
) = {
val outer =
StateT.stateTMonadState[Int, ({ type L[y] = StateT[M, String, y] })#L]

for {
_ <- outer.modify(_ + 1)
_ <- mt.liftMU(inner.modify(_ + "1"))
a <- outer.get
b <- mt.liftMU(inner.get)
} yield (a, b)
}

现在您可以编写以下内容,例如:
scala> test3[Id].eval(0).eval("0")
res0: (Int, String) = (1,01)

就像在 Haskell 示例中一样。

脚注

如果您对提交 Id 感到满意,可以稍微清理一下。作为内部状态转换器的单子(monad)(如您的评论所示):
def test3 = {
val mt = MonadTrans[({ type L[f[_], a] = StateT[f, Int, a] })#L]
val outer = StateT.stateTMonadState[Int, ({ type L[y] = State[String, y] })#L]
for {
_ <- outer.modify(_ + 1)
_ <- mt.liftMU(modify[String](_ + "1"))
a <- outer.get
b <- mt.liftMU(get[String])
} yield (a, b)
}

它不太通用,但它可能对你有用。

关于scala - 在scalaz中堆叠StateT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com