简体   繁体   English

为什么未装箱的数组不是可折叠的实例?

[英]Why are unboxed arrays not an instance of foldable?

Figuring out the correct data container to use can be a bit tricky in Haskell, for my 2D grid application I thought using UArray would be appropriate. 确定要使用的正确数据容器在Haskell中可能有点棘手,因为我认为使用UArray 2D网格应用程序是合适的。 However, as far as I can tell UArray is not an instance of foldable (not in Data.Array.IArray nor Data.Array.Unboxed ). 但是,据我所知, UArray不是foldable的实例(不在Data.Array.IArray也不在Data.Array.Unboxed )。

Is there a reason for this? 是否有一个原因? I can make my own helper functions but the absence of a foldable instance suggests that maybe I shouldn't. 我可以创建自己的辅助函数,但缺少foldable实例表明我可能不应该这样做。

I believe such an instance is impossible, because it requires an extra constraint in the data type contained by the array, which cannot be expressed in Foldable. 我相信这样的实例是不可能的,因为它需要数组包含的数据类型中的额外约束,这不能用Foldable表示。 In mono-traversable, I do define MonoFoldable instances for unboxed and storable vectors. 在单遍历中,我确实为未装箱和可存储的矢量定义了MonoFoldable实例。

EDIT : To be clear, the constraint I'm referring to is that all functions in Data.Vector.Unbox only work if the value contained by the Vector is an instance of Unbox , whereas Foldable requires that foldMap , foldr , etc, are defined for all possible types (as is the case for types like lists, boxed vectors, etc). 编辑 :要清楚,我所指的约束是Data.Vector.Unbox中的所有函数只有在Vector包含的值是Unbox的实例时才有效,而Foldable需要定义foldMapfoldr等。对于所有可能的类型(如列表,盒装向量等类型的情况)。 There is no way with the Foldable typeclass to state "the contained value must meet these constraints." Foldable类型类没有办法声明“包含的值必须满足这些约束”。 With MonoFoldable , there is. 有了MonoFoldable ,就有了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM