@@ -25,7 +25,7 @@ module Control.Concurrent.STM.TArray (
2525import Data.Array (Array , bounds )
2626import Data.Array.Base (listArray , arrEleBottom , unsafeAt , MArray (.. ),
2727 IArray (numElements ))
28- import Data.Ix (rangeSize )
28+ import Data.Ix (Ix ( rangeSize ) )
2929import Data.Typeable (Typeable )
3030import Control.Concurrent.STM.TVar (TVar , newTVar , readTVar , writeTVar )
3131#ifdef __GLASGOW_HASKELL__
@@ -41,7 +41,18 @@ import Control.Sequential.STM (STM)
4141-- but it may be replaced by a more efficient implementation in the future
4242-- (the interface will remain the same, however).
4343--
44- newtype TArray i e = TArray (Array i (TVar e )) deriving (Eq , Typeable )
44+ newtype TArray i e = TArray (Array i (TVar e )) deriving (Typeable )
45+
46+ -- There are no provisions for moving/copying TVars between TArrays.
47+ -- Therefore, two TArrays are equal if and only if they are both empty or are
48+ -- actually the same array in memory. We have no safe operations for checking
49+ -- that directly (though in practice we could use `unsafeCoerce#` with
50+ -- `sameMutableArray#`). So instead we take a quick look at the array sizes and
51+ -- then decide based on the first TVar of each.
52+ instance Ix i => Eq (TArray i e ) where
53+ TArray t1 == TArray t2
54+ = numElements t1 == numElements t2
55+ && (numElements t1 == 0 || unsafeAt t1 0 == unsafeAt t2 0 )
4556
4657instance MArray TArray e STM where
4758 getBounds (TArray a) = return (bounds a)
0 commit comments