From 40022f56cfcf79d56abecf4055d9a5cc3a07447e Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 21 May 2024 11:50:39 +0200 Subject: 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. --- src/Data/Array/Mixed.hs | 3 +++ src/Data/Array/Nested/Internal.hs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) (limited to 'src') 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)) -- cgit v1.2.3-70-g09d2