Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are unboxed arrays not an instance of foldable?

Tags:

arrays

haskell

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.

like image 296
user668074 Avatar asked Mar 31 '16 02:03

user668074


1 Answers

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.

like image 143
Michael Snoyman Avatar answered Sep 28 '22 02:09

Michael Snoyman