[Haskell-cafe] Trouble using unboxed arrays

Justin Bailey jgbailey at gmail.com
Sat Nov 10 14:09:54 EST 2007


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!

Justin


More information about the Haskell-Cafe mailing list