From 18e5aea2b7f7d1df589a9737e61c2cfc937d7a87 Mon Sep 17 00:00:00 2001
From: William Rusnack <williamrusnack@gmail.com>
Date: Sun, 30 Jun 2024 14:20:07 -0400
Subject: [PATCH 1/4] mutable-containers indexing

---
 mutable-containers/src/Data/Mutable.hs       |  2 +
 mutable-containers/src/Data/Mutable/Class.hs | 77 ++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/mutable-containers/src/Data/Mutable.hs b/mutable-containers/src/Data/Mutable.hs
index 3d219dd8..00dc921b 100644
--- a/mutable-containers/src/Data/Mutable.hs
+++ b/mutable-containers/src/Data/Mutable.hs
@@ -39,6 +39,8 @@ module Data.Mutable
     , MutableRef (..)
     , MutableAtomicRef (..)
     , MutableCollection (..)
+    , MutableInitialSizedCollection (..)
+    , MutableIndexing (..)
     , MutablePushFront (..)
     , MutablePushBack (..)
     , MutablePopFront (..)
diff --git a/mutable-containers/src/Data/Mutable/Class.hs b/mutable-containers/src/Data/Mutable/Class.hs
index 67686c11..8be91a2b 100644
--- a/mutable-containers/src/Data/Mutable/Class.hs
+++ b/mutable-containers/src/Data/Mutable/Class.hs
@@ -21,6 +21,8 @@ module Data.Mutable.Class
     , MutableRef (..)
     , MutableAtomicRef (..)
     , MutableCollection (..)
+    , MutableInitialSizedCollection (..)
+    , MutableIndexing (..)
     , MutablePushFront (..)
     , MutablePushBack (..)
     , MutablePopFront (..)
@@ -38,6 +40,10 @@ import           Data.MonoTraversable    (Element)
 import           Data.Primitive.MutVar
 import qualified Data.Sequences          as Seqs
 import           Data.STRef
+import qualified Data.Vector.Mutable               as MV
+import qualified Data.Vector.Primitive.Mutable     as MPV
+import qualified Data.Vector.Storable.Mutable      as MSV
+import qualified Data.Vector.Unboxed.Mutable       as MUV
 
 -- | The parent typeclass for all mutable containers.
 --
@@ -55,6 +61,14 @@ instance MutableContainer (STRef s a) where
     type MCState (STRef s a) = s
 instance MutableContainer (MutVar s a) where
     type MCState (MutVar s a) = s
+instance MutableContainer (MV.MVector s a) where
+    type MCState (MV.MVector s a) = s
+instance MutableContainer (MPV.MVector s a) where
+    type MCState (MPV.MVector s a) = s
+instance MutableContainer (MSV.MVector s a) where
+    type MCState (MSV.MVector s a) = s
+instance MutableContainer (MUV.MVector s a) where
+    type MCState (MUV.MVector s a) = s
 
 -- | Typeclass for single-cell mutable references.
 --
@@ -202,6 +216,69 @@ instance Monoid w => MutableCollection (MutVar s w) where
     type CollElement (MutVar s w) = Element w
     newColl = newRef mempty
     {-# INLINE newColl #-}
+instance MutableCollection (MV.MVector s a) where
+    type CollElement (MV.MVector s a) = a
+    newColl = MV.new 0
+    {-# INLINE newColl #-}
+instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
+    type CollElement (MPV.MVector s a) = a
+    newColl = MPV.new 0
+    {-# INLINE newColl #-}
+instance MSV.Storable a => MutableCollection (MSV.MVector s a) where
+    type CollElement (MSV.MVector s a) = a
+    newColl = MSV.new 0
+    {-# INLINE newColl #-}
+instance MUV.Unbox a => MutableCollection (MUV.MVector s a) where
+    type CollElement (MUV.MVector s a) = a
+    newColl = MUV.new 0
+    {-# INLINE newColl #-}
+
+-- | Containers that can be initialized with n elements.
+class MutableCollection c => MutableInitialSizedCollection c where
+    type CollIndex c
+    newCollOfSize :: (PrimMonad m, PrimState m ~ MCState c)
+            => CollIndex c
+            -> m c
+instance MutableInitialSizedCollection (MV.MVector s a) where
+    type CollIndex (MV.MVector s a) = Int
+    newCollOfSize = MV.new
+    {-# INLINE newCollOfSize #-}
+instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
+    type CollIndex (MPV.MVector s a) = Int
+    newCollOfSize = MPV.new
+    {-# INLINE newCollOfSize #-}
+instance MSV.Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
+    type CollIndex (MSV.MVector s a) = Int
+    newCollOfSize = MSV.new
+    {-# INLINE newCollOfSize #-}
+instance MUV.Unbox a => MutableInitialSizedCollection (MUV.MVector s a) where
+    type CollIndex (MUV.MVector s a) = Int
+    newCollOfSize = MUV.new
+    {-# INLINE newCollOfSize #-}
+
+class MutableInitialSizedCollection c => MutableIndexing c where
+    readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
+    writeIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> CollElement c -> m ()
+instance MutableIndexing (MV.MVector s a) where
+    readIndex = MV.read
+    {-# INLINE readIndex #-}
+    writeIndex = MV.write
+    {-# INLINE writeIndex #-}
+instance MPV.Prim a => MutableIndexing (MPV.MVector s a) where
+    readIndex = MPV.read
+    {-# INLINE readIndex #-}
+    writeIndex = MPV.write
+    {-# INLINE writeIndex #-}
+instance MSV.Storable a => MutableIndexing (MSV.MVector s a) where
+    readIndex = MSV.read
+    {-# INLINE readIndex #-}
+    writeIndex = MSV.write
+    {-# INLINE writeIndex #-}
+instance MUV.Unbox a => MutableIndexing (MUV.MVector s a) where
+    readIndex = MUV.read
+    {-# INLINE readIndex #-}
+    writeIndex = MUV.write
+    {-# INLINE writeIndex #-}
 
 -- | Take a value from the front of the collection, if available.
 --

From cb26ff681cefb0792e8f27c99aa60c72ac25b734 Mon Sep 17 00:00:00 2001
From: William Rusnack <williamrusnack@gmail.com>
Date: Sun, 30 Jun 2024 15:29:58 -0400
Subject: [PATCH 2/4] added STArray

---
 mutable-containers/src/Data/Mutable/Class.hs | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/mutable-containers/src/Data/Mutable/Class.hs b/mutable-containers/src/Data/Mutable/Class.hs
index 8be91a2b..f85c6f0a 100644
--- a/mutable-containers/src/Data/Mutable/Class.hs
+++ b/mutable-containers/src/Data/Mutable/Class.hs
@@ -40,10 +40,12 @@ import           Data.MonoTraversable    (Element)
 import           Data.Primitive.MutVar
 import qualified Data.Sequences          as Seqs
 import           Data.STRef
+import           Control.Monad.ST                  (ST)
 import qualified Data.Vector.Mutable               as MV
 import qualified Data.Vector.Primitive.Mutable     as MPV
 import qualified Data.Vector.Storable.Mutable      as MSV
 import qualified Data.Vector.Unboxed.Mutable       as MUV
+import qualified GHC.Arr
 
 -- | The parent typeclass for all mutable containers.
 --
@@ -69,6 +71,8 @@ instance MutableContainer (MSV.MVector s a) where
     type MCState (MSV.MVector s a) = s
 instance MutableContainer (MUV.MVector s a) where
     type MCState (MUV.MVector s a) = s
+instance MutableContainer (GHC.Arr.STArray s i e) where
+    type MCState (GHC.Arr.STArray s i e) = s
 
 -- | Typeclass for single-cell mutable references.
 --
@@ -232,6 +236,10 @@ instance MUV.Unbox a => MutableCollection (MUV.MVector s a) where
     type CollElement (MUV.MVector s a) = a
     newColl = MUV.new 0
     {-# INLINE newColl #-}
+instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) where
+    type CollElement (GHC.Arr.STArray s i e) = e
+    newColl = primToPrim $ GHC.Arr.newSTArray (0,0) undefined
+    {-# INLINE newColl #-}
 
 -- | Containers that can be initialized with n elements.
 class MutableCollection c => MutableInitialSizedCollection c where
@@ -255,6 +263,10 @@ instance MUV.Unbox a => MutableInitialSizedCollection (MUV.MVector s a) where
     type CollIndex (MUV.MVector s a) = Int
     newCollOfSize = MUV.new
     {-# INLINE newCollOfSize #-}
+instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray s i e) where
+    type CollIndex (GHC.Arr.STArray s i e) = i
+    newCollOfSize x = primToPrim $ GHC.Arr.newSTArray (0,x) undefined
+    {-# INLINE newCollOfSize #-}
 
 class MutableInitialSizedCollection c => MutableIndexing c where
     readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
@@ -279,6 +291,11 @@ instance MUV.Unbox a => MutableIndexing (MUV.MVector s a) where
     {-# INLINE readIndex #-}
     writeIndex = MUV.write
     {-# INLINE writeIndex #-}
+instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
+    readIndex c i = primToPrim $ GHC.Arr.readSTArray c i
+    {-# INLINE readIndex #-}
+    writeIndex c i e = primToPrim $ GHC.Arr.writeSTArray c i e
+    {-# INLINE writeIndex #-}
 
 -- | Take a value from the front of the collection, if available.
 --

From f845c2f5eec8c4806a364aa3b8f1a6ee8ae83041 Mon Sep 17 00:00:00 2001
From: William Rusnack <williamrusnack@gmail.com>
Date: Sun, 30 Jun 2024 16:56:38 -0400
Subject: [PATCH 3/4] added Ptr arrays

---
 mutable-containers/src/Data/Mutable/Class.hs | 25 +++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/mutable-containers/src/Data/Mutable/Class.hs b/mutable-containers/src/Data/Mutable/Class.hs
index f85c6f0a..391ff0f8 100644
--- a/mutable-containers/src/Data/Mutable/Class.hs
+++ b/mutable-containers/src/Data/Mutable/Class.hs
@@ -46,6 +46,10 @@ import qualified Data.Vector.Primitive.Mutable     as MPV
 import qualified Data.Vector.Storable.Mutable      as MSV
 import qualified Data.Vector.Unboxed.Mutable       as MUV
 import qualified GHC.Arr
+import qualified Foreign.Marshal.Array             as Foreign
+import           Foreign.Ptr                       (Ptr)
+import           Foreign.Storable                  (Storable)
+import qualified Foreign.Storable                  as Foreign
 
 -- | The parent typeclass for all mutable containers.
 --
@@ -73,6 +77,8 @@ instance MutableContainer (MUV.MVector s a) where
     type MCState (MUV.MVector s a) = s
 instance MutableContainer (GHC.Arr.STArray s i e) where
     type MCState (GHC.Arr.STArray s i e) = s
+instance MutableContainer (Ptr a) where
+    type MCState (Ptr a) = PrimState IO
 
 -- | Typeclass for single-cell mutable references.
 --
@@ -228,7 +234,7 @@ instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
     type CollElement (MPV.MVector s a) = a
     newColl = MPV.new 0
     {-# INLINE newColl #-}
-instance MSV.Storable a => MutableCollection (MSV.MVector s a) where
+instance Storable a => MutableCollection (MSV.MVector s a) where
     type CollElement (MSV.MVector s a) = a
     newColl = MSV.new 0
     {-# INLINE newColl #-}
@@ -240,6 +246,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) wher
     type CollElement (GHC.Arr.STArray s i e) = e
     newColl = primToPrim $ GHC.Arr.newSTArray (0,0) undefined
     {-# INLINE newColl #-}
+instance Storable a => MutableCollection (Ptr a) where
+    type CollElement (Ptr a) = a
+    newColl = primToPrim $ Foreign.mallocArray 0
+    {-# INLINE newColl #-}
 
 -- | Containers that can be initialized with n elements.
 class MutableCollection c => MutableInitialSizedCollection c where
@@ -255,7 +265,7 @@ instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
     type CollIndex (MPV.MVector s a) = Int
     newCollOfSize = MPV.new
     {-# INLINE newCollOfSize #-}
-instance MSV.Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
+instance Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
     type CollIndex (MSV.MVector s a) = Int
     newCollOfSize = MSV.new
     {-# INLINE newCollOfSize #-}
@@ -267,6 +277,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray
     type CollIndex (GHC.Arr.STArray s i e) = i
     newCollOfSize x = primToPrim $ GHC.Arr.newSTArray (0,x) undefined
     {-# INLINE newCollOfSize #-}
+instance Storable a => MutableInitialSizedCollection (Ptr a) where
+    type CollIndex (Ptr a) = Int
+    newCollOfSize = primToPrim . Foreign.mallocArray 
+    {-# INLINE newCollOfSize #-}
 
 class MutableInitialSizedCollection c => MutableIndexing c where
     readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
@@ -281,7 +295,7 @@ instance MPV.Prim a => MutableIndexing (MPV.MVector s a) where
     {-# INLINE readIndex #-}
     writeIndex = MPV.write
     {-# INLINE writeIndex #-}
-instance MSV.Storable a => MutableIndexing (MSV.MVector s a) where
+instance Storable a => MutableIndexing (MSV.MVector s a) where
     readIndex = MSV.read
     {-# INLINE readIndex #-}
     writeIndex = MSV.write
@@ -296,6 +310,11 @@ instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
     {-# INLINE readIndex #-}
     writeIndex c i e = primToPrim $ GHC.Arr.writeSTArray c i e
     {-# INLINE writeIndex #-}
+instance Storable a => MutableIndexing (Ptr a) where
+    readIndex p i = primToPrim $ Foreign.peekElemOff p i
+    {-# INLINE readIndex #-}
+    writeIndex p i e = primToPrim $ Foreign.pokeElemOff p i e
+    {-# INLINE writeIndex #-}
 
 -- | Take a value from the front of the collection, if available.
 --

From cc188b5a5034a62e75497f48af19053dc86c3a15 Mon Sep 17 00:00:00 2001
From: William Rusnack <williamrusnack@gmail.com>
Date: Mon, 1 Jul 2024 18:12:58 -0400
Subject: [PATCH 4/4] added Mutable Primiative Arrays

---
 mutable-containers/mutable-containers.cabal  |  3 +-
 mutable-containers/src/Data/Mutable.hs       |  7 +-
 mutable-containers/src/Data/Mutable/Array.hs | 72 ++++++++++++++++++
 mutable-containers/src/Data/Mutable/Class.hs | 80 ++++++++++----------
 4 files changed, 117 insertions(+), 45 deletions(-)
 create mode 100644 mutable-containers/src/Data/Mutable/Array.hs

diff --git a/mutable-containers/mutable-containers.cabal b/mutable-containers/mutable-containers.cabal
index 2cd3d339..8527e496 100644
--- a/mutable-containers/mutable-containers.cabal
+++ b/mutable-containers/mutable-containers.cabal
@@ -1,6 +1,6 @@
 cabal-version: 1.12
 
--- This file has been generated from package.yaml by hpack version 0.34.7.
+-- This file has been generated from package.yaml by hpack version 0.36.0.
 --
 -- see: https://github.com/sol/hpack
 
@@ -28,6 +28,7 @@ library
   exposed-modules:
       Data.Mutable
   other-modules:
+      Data.Mutable.Array
       Data.Mutable.BRef
       Data.Mutable.Class
       Data.Mutable.Deque
diff --git a/mutable-containers/src/Data/Mutable.hs b/mutable-containers/src/Data/Mutable.hs
index 00dc921b..dcf191a8 100644
--- a/mutable-containers/src/Data/Mutable.hs
+++ b/mutable-containers/src/Data/Mutable.hs
@@ -34,13 +34,15 @@ module Data.Mutable
     , asBDeque
     , DLList
     , asDLList
+    , Array (..)
+    , ArrayMemoryProperties (..)
       -- * Type classes
     , MutableContainer (..)
     , MutableRef (..)
     , MutableAtomicRef (..)
     , MutableCollection (..)
-    , MutableInitialSizedCollection (..)
-    , MutableIndexing (..)
+    , MutableAllocatedCollection (..)
+    , MutableIndexingWrite (..)
     , MutablePushFront (..)
     , MutablePushBack (..)
     , MutablePopFront (..)
@@ -65,6 +67,7 @@ import Data.Mutable.PRef
 import Data.Mutable.BRef
 import Data.Mutable.Deque
 import Data.Mutable.DLList
+import Data.Mutable.Array
 import Data.Vector.Unboxed (Unbox)
 import Data.Primitive (Prim)
 import Data.Vector.Storable (Storable)
diff --git a/mutable-containers/src/Data/Mutable/Array.hs b/mutable-containers/src/Data/Mutable/Array.hs
new file mode 100644
index 00000000..9c73ef5f
--- /dev/null
+++ b/mutable-containers/src/Data/Mutable/Array.hs
@@ -0,0 +1,72 @@
+{-# LANGUAGE KindSignatures #-}
+{-# LANGUAGE ConstraintKinds #-}
+{-# LANGUAGE UndecidableInstances #-}
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE StandaloneKindSignatures #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TypeApplications #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE FlexibleContexts #-}
+module Data.Mutable.Array
+    ( Array (..)
+    , ArrayMemoryProperties (..)
+    ) where
+
+import Data.Mutable.Class
+import Data.Word
+import GHC.TypeLits
+import Data.Kind (Constraint)
+import Data.Proxy (Proxy(Proxy))
+import Unsafe.Coerce (unsafeCoerce)
+import Control.Monad.Primitive
+
+import Data.Primitive.ByteArray (MutableByteArray, newByteArray, newPinnedByteArray, newAlignedPinnedByteArray, writeByteArray)
+import Data.Primitive.Types (Prim)
+
+newtype Array (p :: ArrayMemoryProperties) e s = Array (MutableByteArray s)
+
+data ArrayMemoryProperties = Regular | Pinned | AlignedPinned Nat
+
+instance MutableContainer (Array p e s) where
+    type MCState (Array p e s) = s
+
+instance MutableCollection (Array Regular e s) where
+    type CollElement (Array Regular e s) = e
+    newColl = coerceToArray $ newByteArray 0
+instance MutableCollection (Array Pinned e s) where
+    type CollElement (Array Pinned e s) = e
+    newColl = coerceToArray $ newPinnedByteArray 0
+instance KnownNat n => MutableCollection (Array (AlignedPinned n) e s) where
+    type CollElement (Array (AlignedPinned n) e s) = e
+    newColl = coerceToArray $ newAlignedPinnedByteArray 0 alignment
+      where
+      alignment = fromIntegral $ natVal $ Proxy @n
+
+type instance CollIndex (Array _ _ _) = Int
+instance MutableAllocatedCollection (Array Regular e s) where
+    newCollOfSize = coerceToArray . newByteArray
+    {-# INLINE newCollOfSize #-}
+instance MutableAllocatedCollection (Array Pinned e s) where
+    newCollOfSize = coerceToArray . newPinnedByteArray
+    {-# INLINE newCollOfSize #-}
+instance KnownNat n => MutableAllocatedCollection (Array (AlignedPinned n) e s) where
+    newCollOfSize = coerceToArray . flip newAlignedPinnedByteArray alignment
+      where
+      alignment = fromIntegral $ natVal $ Proxy @n
+    {-# INLINE newCollOfSize #-}
+
+coerceToArray :: m (MutableByteArray s) -> m (Array p e s)
+coerceToArray = unsafeCoerce
+
+instance (Prim (CollElement (Array p e s)), MutableAllocatedCollection (Array p e s)) => MutableIndexingWrite (Array p e s) where
+  writeIndex (Array c) i x = writeByteArray c i x
+
+type IsPow2 :: Nat -> Constraint
+type IsPow2 x = IsPow2' (Mod x 2) x
+type IsPow2' :: Nat -> Nat -> Constraint
+type family IsPow2' m x where
+  IsPow2' _ 2 = ()
+  IsPow2' 1 x = TypeError (ShowType x :<>: Text " is not a power of 2.")
+  IsPow2' 0 x = IsPow2' 0 (Div x 2)
diff --git a/mutable-containers/src/Data/Mutable/Class.hs b/mutable-containers/src/Data/Mutable/Class.hs
index 391ff0f8..7f49d37b 100644
--- a/mutable-containers/src/Data/Mutable/Class.hs
+++ b/mutable-containers/src/Data/Mutable/Class.hs
@@ -21,8 +21,9 @@ module Data.Mutable.Class
     , MutableRef (..)
     , MutableAtomicRef (..)
     , MutableCollection (..)
-    , MutableInitialSizedCollection (..)
-    , MutableIndexing (..)
+    , MutableAllocatedCollection (..)
+    , CollIndex
+    , MutableIndexingWrite (..)
     , MutablePushFront (..)
     , MutablePushBack (..)
     , MutablePopFront (..)
@@ -229,90 +230,85 @@ instance Monoid w => MutableCollection (MutVar s w) where
 instance MutableCollection (MV.MVector s a) where
     type CollElement (MV.MVector s a) = a
     newColl = MV.new 0
-    {-# INLINE newColl #-}
 instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
     type CollElement (MPV.MVector s a) = a
     newColl = MPV.new 0
-    {-# INLINE newColl #-}
 instance Storable a => MutableCollection (MSV.MVector s a) where
     type CollElement (MSV.MVector s a) = a
     newColl = MSV.new 0
-    {-# INLINE newColl #-}
 instance MUV.Unbox a => MutableCollection (MUV.MVector s a) where
     type CollElement (MUV.MVector s a) = a
     newColl = MUV.new 0
-    {-# INLINE newColl #-}
 instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) where
     type CollElement (GHC.Arr.STArray s i e) = e
     newColl = primToPrim $ GHC.Arr.newSTArray (0,0) undefined
-    {-# INLINE newColl #-}
 instance Storable a => MutableCollection (Ptr a) where
     type CollElement (Ptr a) = a
     newColl = primToPrim $ Foreign.mallocArray 0
-    {-# INLINE newColl #-}
 
 -- | Containers that can be initialized with n elements.
-class MutableCollection c => MutableInitialSizedCollection c where
-    type CollIndex c
+type family CollIndex c
+
+class MutableCollection c => MutableAllocatedCollection c where
     newCollOfSize :: (PrimMonad m, PrimState m ~ MCState c)
             => CollIndex c
             -> m c
-instance MutableInitialSizedCollection (MV.MVector s a) where
-    type CollIndex (MV.MVector s a) = Int
+type instance CollIndex (MV.MVector s a) = Int
+instance MutableAllocatedCollection (MV.MVector s a) where
     newCollOfSize = MV.new
     {-# INLINE newCollOfSize #-}
-instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
-    type CollIndex (MPV.MVector s a) = Int
+type instance CollIndex (MPV.MVector s a) = Int
+instance MPV.Prim a => MutableAllocatedCollection (MPV.MVector s a) where
     newCollOfSize = MPV.new
     {-# INLINE newCollOfSize #-}
-instance Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
-    type CollIndex (MSV.MVector s a) = Int
+type instance CollIndex (MSV.MVector s a) = Int
+instance Storable a => MutableAllocatedCollection (MSV.MVector s a) where
     newCollOfSize = MSV.new
     {-# INLINE newCollOfSize #-}
-instance MUV.Unbox a => MutableInitialSizedCollection (MUV.MVector s a) where
-    type CollIndex (MUV.MVector s a) = Int
+type instance CollIndex (MUV.MVector s a) = Int
+instance MUV.Unbox a => MutableAllocatedCollection (MUV.MVector s a) where
     newCollOfSize = MUV.new
     {-# INLINE newCollOfSize #-}
-instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray s i e) where
-    type CollIndex (GHC.Arr.STArray s i e) = i
+type instance CollIndex (GHC.Arr.STArray s i e) = i
+instance (GHC.Arr.Ix i, Num i) => MutableAllocatedCollection (GHC.Arr.STArray s i e) where
     newCollOfSize x = primToPrim $ GHC.Arr.newSTArray (0,x) undefined
     {-# INLINE newCollOfSize #-}
-instance Storable a => MutableInitialSizedCollection (Ptr a) where
-    type CollIndex (Ptr a) = Int
+type instance CollIndex (Ptr a) = Int
+instance Storable a => MutableAllocatedCollection (Ptr a) where
     newCollOfSize = primToPrim . Foreign.mallocArray 
     {-# INLINE newCollOfSize #-}
 
-class MutableInitialSizedCollection c => MutableIndexing c where
-    readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
+class MutableAllocatedCollection c => MutableIndexingWrite c where
+--    readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
     writeIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> CollElement c -> m ()
-instance MutableIndexing (MV.MVector s a) where
-    readIndex = MV.read
-    {-# INLINE readIndex #-}
+instance MutableIndexingWrite (MV.MVector s a) where
+--    readIndex = MV.read
+--    {-# INLINE readIndex #-}
     writeIndex = MV.write
     {-# INLINE writeIndex #-}
-instance MPV.Prim a => MutableIndexing (MPV.MVector s a) where
-    readIndex = MPV.read
-    {-# INLINE readIndex #-}
+instance MPV.Prim a => MutableIndexingWrite (MPV.MVector s a) where
+--    readIndex = MPV.read
+--    {-# INLINE readIndex #-}
     writeIndex = MPV.write
     {-# INLINE writeIndex #-}
-instance Storable a => MutableIndexing (MSV.MVector s a) where
-    readIndex = MSV.read
-    {-# INLINE readIndex #-}
+instance Storable a => MutableIndexingWrite (MSV.MVector s a) where
+--    readIndex = MSV.read
+--    {-# INLINE readIndex #-}
     writeIndex = MSV.write
     {-# INLINE writeIndex #-}
-instance MUV.Unbox a => MutableIndexing (MUV.MVector s a) where
-    readIndex = MUV.read
-    {-# INLINE readIndex #-}
+instance MUV.Unbox a => MutableIndexingWrite (MUV.MVector s a) where
+--    readIndex = MUV.read
+--    {-# INLINE readIndex #-}
     writeIndex = MUV.write
     {-# INLINE writeIndex #-}
-instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
-    readIndex c i = primToPrim $ GHC.Arr.readSTArray c i
-    {-# INLINE readIndex #-}
+instance (GHC.Arr.Ix i, Num i) => MutableIndexingWrite (GHC.Arr.STArray s i e) where
+--    readIndex c i = primToPrim $ GHC.Arr.readSTArray c i
+--    {-# INLINE readIndex #-}
     writeIndex c i e = primToPrim $ GHC.Arr.writeSTArray c i e
     {-# INLINE writeIndex #-}
-instance Storable a => MutableIndexing (Ptr a) where
-    readIndex p i = primToPrim $ Foreign.peekElemOff p i
-    {-# INLINE readIndex #-}
+instance Storable a => MutableIndexingWrite (Ptr a) where
+--    readIndex p i = primToPrim $ Foreign.peekElemOff p i
+--    {-# INLINE readIndex #-}
     writeIndex p i e = primToPrim $ Foreign.pokeElemOff p i e
     {-# INLINE writeIndex #-}