aboutsummaryrefslogtreecommitdiff
path: root/src/Data/Array
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-05-21 11:50:39 +0200
committerTom Smeding <tom@tomsmeding.com>2024-05-21 11:50:47 +0200
commit40022f56cfcf79d56abecf4055d9a5cc3a07447e (patch)
treed278b3d0ee91e096f3568b3c17b17108813f2b06 /src/Data/Array
parent3d2e4a567668ea951e629834e6871a3f144c1b84 (diff)
Add Ord instances on 0-dimensional arrays
This is very conservative; of course a law-abiding Ord instance can be defined on full multi-dimensional arrays, but such an instance would be lexicographical (anything else is even stranger), and that is potentially unexpected and definitely does not play well with the other numeric classes.
Diffstat (limited to 'src/Data/Array')
-rw-r--r--src/Data/Array/Mixed.hs3
-rw-r--r--src/Data/Array/Nested/Internal.hs14
2 files changed, 17 insertions, 0 deletions
diff --git a/src/Data/Array/Mixed.hs b/src/Data/Array/Mixed.hs
index 320a393..baa9575 100644
--- a/src/Data/Array/Mixed.hs
+++ b/src/Data/Array/Mixed.hs
@@ -289,6 +289,9 @@ type XArray :: [Maybe Nat] -> Type -> Type
newtype XArray sh a = XArray (S.Array (Rank sh) a)
deriving (Show, Eq)
+-- | Only on scalars, because lexicographical ordering is strange on multi-dimensional arrays.
+deriving instance (Ord a, Storable a) => Ord (XArray '[] a)
+
zeroIxX :: StaticShX sh -> IIxX sh
zeroIxX ZKX = ZIX
zeroIxX (_ :!% ssh) = 0 :.% zeroIxX ssh
diff --git a/src/Data/Array/Nested/Internal.hs b/src/Data/Array/Nested/Internal.hs
index b645f4a..933ce6c 100644
--- a/src/Data/Array/Nested/Internal.hs
+++ b/src/Data/Array/Nested/Internal.hs
@@ -463,6 +463,9 @@ data family Mixed sh a
data instance Mixed sh (Primitive a) = M_Primitive !(IShX sh) !(XArray sh a)
deriving (Show, Eq)
+-- | Only on scalars, because lexicographical ordering is strange on multi-dimensional arrays.
+deriving instance (Ord a, Storable a) => Ord (Mixed '[] (Primitive a))
+
-- [PRIMITIVE ELEMENT TYPES LIST]
newtype instance Mixed sh Int = M_Int (Mixed sh (Primitive Int)) deriving (Show, Eq)
newtype instance Mixed sh Int64 = M_Int64 (Mixed sh (Primitive Int64)) deriving (Show, Eq)
@@ -473,6 +476,15 @@ newtype instance Mixed sh Double = M_Double (Mixed sh (Primitive Double)) derivi
newtype instance Mixed sh () = M_Nil (Mixed sh (Primitive ())) deriving (Show, Eq) -- no content, orthotope optimises this (via Vector)
-- etc.
+-- [PRIMITIVE ELEMENT TYPES LIST]
+deriving instance Ord (Mixed '[] Int)
+deriving instance Ord (Mixed '[] Int64)
+deriving instance Ord (Mixed '[] Int32)
+deriving instance Ord (Mixed '[] CInt)
+deriving instance Ord (Mixed '[] Float)
+deriving instance Ord (Mixed '[] Double)
+deriving instance Ord (Mixed '[] ())
+
data instance Mixed sh (a, b) = M_Tup2 !(Mixed sh a) !(Mixed sh b)
deriving instance (Show (Mixed sh a), Show (Mixed sh b)) => Show (Mixed sh (a, b))
-- etc.
@@ -1060,6 +1072,7 @@ type Ranked :: Nat -> Type -> Type
newtype Ranked n a = Ranked (Mixed (Replicate n Nothing) a)
deriving instance Show (Mixed (Replicate n Nothing) a) => Show (Ranked n a)
deriving instance Eq (Mixed (Replicate n Nothing) a) => Eq (Ranked n a)
+deriving instance Ord (Mixed '[] a) => Ord (Ranked 0 a)
-- | A shape-typed array: the full shape of the array (the sizes of its
-- dimensions) is represented on the type level as a list of 'Nat's. Note that
@@ -1074,6 +1087,7 @@ type Shaped :: [Nat] -> Type -> Type
newtype Shaped sh a = Shaped (Mixed (MapJust sh) a)
deriving instance Show (Mixed (MapJust sh) a) => Show (Shaped sh a)
deriving instance Eq (Mixed (MapJust sh) a) => Eq (Shaped sh a)
+deriving instance Ord (Mixed '[] a) => Ord (Shaped '[] a)
-- just unwrap the newtype and defer to the general instance for nested arrays
newtype instance Mixed sh (Ranked n a) = M_Ranked (Mixed sh (Mixed (Replicate n Nothing) a))