[Haskell-cafe] Trouble using unboxed arrays

Stefan O'Rear stefanor at cox.net
Sat Nov 10 14:33:58 EST 2007


On Sat, Nov 10, 2007 at 11:09:54AM -0800, Justin Bailey wrote:
> I would like to create a data structure that uses an unboxed array as
> one of its components. I would like the data structure to be
> parameterized over the type of the elements of the array. Further, I'd
> like to build the array using runSTUArray. I can't make the code work
> though. My naive approach:
> 
>   data Ring a = Ring (UArray Int a)
> 
> and the code to make the array:
> 
>   makeArray :: [a] -> Ring a
>   makeArray ls = Ring (ST.runSTUArray (ST.newListArray (0, length ls - 1) ls))
> 
> But that doesn't work. I get from GHC (6.8.1):
> 
>   Could not deduce (MArray (STUArray s) a (ST s)) from the context ()
>     arising from a use of `newListArray'
> 
> I am pretty sure I need to constrain 'a' to primitive types only, but
> how? Can I do it in the data definition?
> 
> Thanks in advance for any help!

What you're trying to do deliberately can't be done.  Polymorphism has a
runtime cost in GHC, and UArrays must specialize.  That said, have you
heard of Data.Array.IArray.listArray?

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071110/38e1e4f3/attachment.bin


More information about the Haskell-Cafe mailing list