简体   繁体   中英

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. However, as far as I can tell UArray is not an instance of foldable (not in Data.Array.IArray nor 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.

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. In mono-traversable, I do define MonoFoldable instances for unboxed and storable vectors.

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). There is no way with the Foldable typeclass to state "the contained value must meet these constraints." With MonoFoldable , there is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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