本文介绍了为什么ListT monad变压器被认为是越野车 - 它打破了哪些monad法则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

这可以通过一个简单的例子来证明吗?



编辑: c> ListT [] 有点不对,我错过了要求内部monad是可交换的。那么,是否有这个要求,还是存在另一个问题,是 ListT 越野车? (Haskell wiki上的全都使用 ListT IO IO 显然是不可交换的。)

解决方案

如何违反关联法:

  v :: Int  - > ListT [] Int 
v 0 = ListT [[0,1]]
v 1 = ListT [[0],[1]]

main = do
print $ runListT $((v> => v)> = v)0
- = [[0,1,0,0,1],[0,1,1,0, 1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1]]
print $ runListT $(v> =>(v> = v))0
- = [[0,1,0,0,1],[0,1,0,0],[ 0,1,0,1],[0,1,1,0,1],[0,1,1,0],[0,1,1,1]]

更多示例(主要使用 IO )以及如何解决 ListT 可以在中找到。


I've seen mentioned that

Can this be demonstrated by a simple example?

Edit: My idea with ListT [] is a bit wrong, I missed that the documentation requires the inner monad to be commutative. So, is ListT buggy just in the sense that has this requirement, or is there another problem? (The examples at Haskell wiki all use ListT IO and IO is obviously not commutative.)

解决方案

A simple example that shows how it fails the associativity law:

v :: Int -> ListT [] Int
v 0 = ListT [[0, 1]]
v 1 = ListT [[0], [1]]

main = do
    print $ runListT $ ((v >=> v) >=> v) 0
    -- = [[0,1,0,0,1],[0,1,1,0,1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1]]
    print $ runListT $ (v >=> (v >=> v)) 0
    -- = [[0,1,0,0,1],[0,1,0,0],[0,1,0,1],[0,1,1,0,1],[0,1,1,0],[0,1,1,1]]

More examples (mostly using IO) and a solution how to fix ListT can be found at ListT done right.

这篇关于为什么ListT monad变压器被认为是越野车 - 它打破了哪些monad法则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 23:46