Improved interface to UArray

Zemyla zemyla at gmail.com
Mon Feb 24 14:45:54 UTC 2020


It's currently hard to describe that a type should be usable with UArray or
IOUArray, and almost impossible to describe without QualifiedConstraints
that it should be usable with STUArray. And it's actually impossible to use
GeneralizedNewtypeDeriving to derive new instances of IArray or MArray. It
also means that it's difficult to keep the three sets of instances in sync.

Therefore, there should be (taking inspiration from primitive) a class that
describes types which can be used in unboxed arrays. It'd be something
along the lines of:

class Unboxed e where
  unsafeNewUArray :: Int# -> e -> State# s -> (# State# s,
MutableByteArray# s #)
  unsafeNewUArray_ :: Const (Int# -> State# s -> (# State# s,
MutableByteArray# s #)) e
  unsafeReadUArray :: MutableByteArray# s -> Int# -> State# s -> (# State#
s, e #)
  unsafeWriteUArray :: MutableByteArray# s -> Int# -> e -> State# s ->
State# s
  unsafeIndexUArray :: ByteArray# -> Int# -> e

And then the instances for UArray, STUArray, and IOUArray would be:

instance Unboxed e => IArray UArray e where ...
instance Unboxed e => MArray IOUArray e IO where ...
instance Unboxed e => MArray (STUArray s) e (ST s) where ...

Incidentally, this also means it'd be less effort to add instances like
Unboxed CInt, Unboxed a => Unboxed (Complex a), and Unboxed Ordering.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20200224/0140b931/attachment.html>


More information about the Libraries mailing list