[Haskell-cafe] No Derived Read for Unboxed Arrays

Matthew Bromberg mattcbro at earthlink.net
Thu Jan 25 16:05:00 EST 2007

I was trying to convert some code from ordinary boxed array to unboxed 
arrays for performance reasons.
Presumably the conversion, according to the GHC documentation should 
only involve appropriate type signatures
(e.g. using UArray instead of Array) and importing Data.Array.Unboxed.  
However my code ultimately failed because
I load a large array saved as a text file using the derived Read, Show 
mechanism.  For some reason there is no Read function
for unboxed arrays.  Here is a little GHCI session showing the problem:

Prelude> :m Data.Array.Unboxed
Prelude Data.Array.Unboxed> let e = listArray (0,3) [0 .. 3] :: UArray 
Int Int
Prelude Data.Array.Unboxed> let se = show e
Prelude Data.Array.Unboxed> se
"array (0,3) [(0,0),(1,1),(2,2),(3,3)]"

Prelude Data.Array.Unboxed> let e2 = read se :: UArray Int Int

    No instance for (Read (UArray Int Int))
      arising from use of `read' at <interactive>:1:9-15
    Possible fix:
      add an instance declaration for (Read (UArray Int Int))
    In the expression: read se
    In the expression: read se :: UArray Int Int
    In the definition of `e2': e2 = read se :: UArray Int Int

However if I try to read it in as boxed arrays, no problem:
Prelude Data.Array.Unboxed> let e2 = read se :: Array Int Int
Prelude Data.Array.Unboxed> e2
array (0,3) [(0,0),(1,1),(2,2),(3,3)]

Is there a reason why this doesn't work?  Is it a design choice or does 
it merely reflect the fact that no one has gotten around to writing a 
read function for unboxed arrays.  I imagine it would be trivial to fix 
this however.

More information about the Haskell-Cafe mailing list