[Haskell-cafe] Data.IArray rant

Evan Laforge qdunkan at gmail.com
Sat Sep 3 03:38:55 CEST 2011


I never liked Data.IArray.  Recently I had to deal with it again and
it kind of touched off a bit of a rant:

Array bounds are an inclusive range, which runs counter to almost all
other uses of ranges, and for good reason.  Length of an array is
(high - low + 1).  If you want to convert a list to an array it's
'IArray.listArray (0, length xs - 1) xs'.  And if you forget to
subtract one or add one it's likely to be a runtime crash.  Off-by-one
errors and runtime crashes... I've gotten accustomed to not having
those in haskell!

The vast majority of data types have their own set of monomorphic
fuctions, and yet array all of the sudden wants to be an interface.
That's nice and all, but who actually uses that interface?  I recall
there used to be a DiffArray and some other variants, but they were
decried as being slow and appear to be gone now.

So it appears that there's a complexity price paid to support
different array types under the same interface, namely giant class
contexts that can't be abstracted away and dependence on MPTCs... and
appears to be unused.  And then another price paid to support
arbitrary indices... which I at least have never used.  Maybe there is
a field where arrays with strange index ranges like 5--20 are useful,
but I always just want 0--length-1.  Meanwhile, they lack basic
features like concatenation.  No Monoid instance.  In fact, hardly any
useful instances.  The result is that my first contact with haskell
arrays left me with the impression that they were complicated, hard to
use, and designed for someone with different priorities than me.  Of
course, Data.Vector didn't exist back then, but if someone were new to
haskell now I would recommend they skip Data.IArray and head straight
for vector.  If they wanted 2 dimensional arrays then I know there are
some matrix libraries.

Of course, sometimes IArray is hard to avoid, e.g. Data.Graph is just
a type synonym for an array.  I wrote a small library of graph
utilities, and dealing with the arrays was back to awkward land.

So... am I alone in this dislike of IArray?  Or maybe someone can
explain why it's actually a very useful interface?  Admittedly vector
is a mere 'cabal install' away, but array has a blessed status as a
ghc bundled library and is likely to be the first place people will
look, as well as the natural base for other data types that want an
array, like Data.Graph.

I know it's not as simple as "just debundle IArray" because it's a
bootlib and presumably used in ghc itself, though I know there is
support for reducing the set of bootlibs.  There's also the whole
mutable interface, which I haven't used, but maybe I'd like it more.
I know the array stuff winds up integrated fairly deeply with ghc's
unboxed arrays and whatnot, so there's a lot of detail I don't
understand.  I just know I dread going back to array using code while
I tend to enjoy working with vector, bytestring, text, storablevector,
etc.



More information about the Haskell-Cafe mailing list