为什么Data.Vector.Mutable read()由于其不可变的操作而在monad中返回? [英] Why does Data.Vector.Mutable read() return in the monad since its an immutable operation?
问题描述
在此处查看 http://hackage.haskell.org/package/vector-0.12.0.3/docs/Data-Vector-Mutable.html 可以看到读取的类型是:
Looking here http://hackage.haskell.org/package/vector-0.12.0.3/docs/Data-Vector-Mutable.html One can see that the type of read is:
read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
由于读操作不会修改向量,所以我的主要问题是为什么不这样做:
Since read operation does not modify the vector, my main question is why is it not:
read :: PrimMonad m => MVector (PrimState m) a -> Int -> a
可变向量的长度也在向量上做不可变的事情,其类型为 MVector s a->.Int
看起来很正常.不是 PrimMonad m =>.MVector(PrimState m)->m Int
.那么为什么读取和长度之间的设计选择有所不同,因为它们都是向量上的不变操作?
The length of a mutable vector is also doing something immutable on a vector, and its type is MVector s a -> Int
which looks normal. It is not PrimMonad m => MVector (PrimState m) a -> m Int
.
So why was this difference of design choice made between read and length since they are both immutable operations on a vector?
现在我考虑一下,通过读取返回的单元格是否可能是对向量内部单元格的引用,而不是其数据的副本?如果是这样,我怎样才能很好地,廉价地获得可变向量中第n个元素的不可变访问权?我正在学习haskell,对细节不太了解.
Now that I think about it, could it be somehow that the cell returned by read is a reference to the cell inside the vector and not a copy of its data? If so how can I nicely and cheaply get immutable access to the n-th element in a mutable vector? I'm learning haskell and not too sure about details.
谢谢
推荐答案
假设
read :: MVector s a -> Int -> a
这意味着 read
是纯净的.考虑
which means read
is pure. Consider
main :: IO ()
main = do
cell <- replicate 1 'a' -- cell = ['a']
print $ read cell 1 -- we want to print 'a'
write cell 1 'z' -- cell = ['z']
print $ read cell 1 -- we want to print 'z'
出了点问题:我写了两次 read cell 1
,并传递了相同的 cell
和 1
参数,因此这两个调用应返回相同的值.这就是 read
是纯净的意思.以上应等于
Something's gone wrong: I wrote read cell 1
twice, passing the same cell
and 1
arguments, so the two calls should return the same value. That's what it means for read
to be pure. The above should be equal to
main :: IO ()
main = do
cell <- replicate 1 'a' -- cell = ['a']
let contents = read cell 1 -- contents = 'a'
print contents -- prints 'a'
write cell 1 'z' -- cell = ['z']; definitely should not affect contents
print contents -- prints 'a'
但是我们不希望这样:我们希望 read
返回不同的内容,即使我们传递相同的参数时,也要考虑到可能发生的任何 write
在两者之间.因此, read
必须是单子动作.
But we don't want that: we want read
to return different things even when we pass the same arguments, taking into account any write
s that might have happened in between. Therefore, read
must be a monadic action.
这与 length
不同.即使向量是可变的,向量的长度也不会改变.在创建时固定的长度.因此, length
是一个纯函数;在创建矢量和查询其长度之间执行了什么单调操作都无关紧要;它将永远是相同的.
This is different from length
. A vector's length never changes, even if the vector is mutable; the length fixed at creation. Therefore, length
is a pure function; it doesn't matter what monadic actions you've performed between creating a vector and querying its length; it will always be the same.
这篇关于为什么Data.Vector.Mutable read()由于其不可变的操作而在monad中返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!