From 901b42ea1bc0dad9d7d915797b7de7594714ae03 Mon Sep 17 00:00:00 2001 From: Georgi Lyubenov Date: Tue, 15 Aug 2023 18:22:05 +0300 Subject: [PATCH] Add Ord instances for Interval, IntervalSet and IntervalMap The primary intended use case for these is to allow storage in maps and sets. --- src/Data/Interval/Internal.hs | 9 ++++++++- src/Data/IntervalMap/Base.hs | 9 ++++++++- src/Data/IntervalSet.hs | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Data/Interval/Internal.hs b/src/Data/Interval/Internal.hs index 9094ccf..16890a4 100644 --- a/src/Data/Interval/Internal.hs +++ b/src/Data/Interval/Internal.hs @@ -55,7 +55,14 @@ data Interval r | LeftOpen !r !r | RightOpen !r !r | BothOpen !r !r - deriving (Eq, Typeable) + deriving + ( Eq, + Ord, + -- ^ Note that this Ord is derived and not semantically meaningful. + -- The primary intended use case is to allow using 'Interval' + -- in maps and sets that require ordering. + Typeable + ) peekInterval :: (Applicative m, Monad m, Ord r) => m Int8 -> m r -> m r -> m (Interval r) peekInterval tagM x y = do diff --git a/src/Data/IntervalMap/Base.hs b/src/Data/IntervalMap/Base.hs index 7322cd7..556e763 100644 --- a/src/Data/IntervalMap/Base.hs +++ b/src/Data/IntervalMap/Base.hs @@ -123,7 +123,14 @@ import qualified GHC.Exts as GHCExts -- Unlike 'IntervalSet', 'IntervalMap' never merge adjacent mappings, -- even if adjacent intervals are connected and mapped to the same value. newtype IntervalMap r a = IntervalMap (Map (LB r) (Interval r, a)) - deriving (Eq, Typeable) + deriving + ( Eq, + Ord, + -- ^ Note that this Ord is derived and not semantically meaningful. + -- The primary intended use case is to allow using 'IntervalSet' + -- in maps and sets that require ordering. + Typeable + ) #if __GLASGOW_HASKELL__ >= 708 type role IntervalMap nominal representational diff --git a/src/Data/IntervalSet.hs b/src/Data/IntervalSet.hs index 226df9a..7e44647 100644 --- a/src/Data/IntervalSet.hs +++ b/src/Data/IntervalSet.hs @@ -88,7 +88,14 @@ import qualified GHC.Exts as GHCExts -- -- Any connected intervals are merged together, and empty intervals are ignored. newtype IntervalSet r = IntervalSet (Map (Extended r) (Interval r)) - deriving (Eq, Typeable) + deriving + ( Eq, + Ord, + -- ^ Note that this Ord is derived and not semantically meaningful. + -- The primary intended use case is to allow using 'IntervalSet' + -- in maps and sets that require ordering. + Typeable + ) #if __GLASGOW_HASKELL__ >= 708 type role IntervalSet nominal