summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Beacham <dbeacham@dbeacham.co.uk>2020-10-06 13:31:09 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-10-27 13:59:35 -0400
commit9ad51bc9d2ad9168abad271f715ce73d3562218a (patch)
tree2c2e99d2d279bd0bb293245f15fa121065baaaff
parent730bb59086ad1036143983c3fba61bd851bebc03 (diff)
downloadhaskell-9ad51bc9d2ad9168abad271f715ce73d3562218a.tar.gz
Fix `instance Bounded a => Bounded (Down a)` (#18716)
* Flip `minBound` and `maxBound` to respect the change in ordering * Remove awkward `Enum` (and hence `Integral`) instances for `Data.Ord.Down` * Update changelog
-rw-r--r--libraries/base/Data/Ord.hs35
-rw-r--r--libraries/base/changelog.md3
2 files changed, 32 insertions, 6 deletions
diff --git a/libraries/base/Data/Ord.hs b/libraries/base/Data/Ord.hs
index 88c22798a7..7628e017b6 100644
--- a/libraries/base/Data/Ord.hs
+++ b/libraries/base/Data/Ord.hs
@@ -28,11 +28,11 @@ import Data.Bits (Bits, FiniteBits)
import Foreign.Storable (Storable)
import GHC.Ix (Ix)
import GHC.Base
-import GHC.Enum (Bounded, Enum)
+import GHC.Enum (Bounded(..))
import GHC.Float (Floating, RealFloat)
import GHC.Num
import GHC.Read
-import GHC.Real (Fractional, Integral, Real, RealFrac)
+import GHC.Real (Fractional, Real, RealFrac)
import GHC.Show
-- |
@@ -66,10 +66,29 @@ clamp (low, high) a = min high (max a low)
-- | The 'Down' type allows you to reverse sort order conveniently. A value of type
-- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
+--
-- If @a@ has an @'Ord'@ instance associated with it then comparing two
-- values thus wrapped will give you the opposite of their normal sort order.
-- This is particularly useful when sorting in generalised list comprehensions,
--- as in: @then sortWith by 'Down' x@
+-- as in: @then sortWith by 'Down' x@.
+--
+-- >>> compare True False
+-- GT
+--
+-- >>> compare (Down True) (Down False)
+-- LT
+--
+-- If @a@ has a @'Bounded'@ instance then the wrapped instance also respects
+-- the reversed ordering by exchanging the values of @'minBound'@ and
+-- @'maxBound'@.
+--
+-- >>> minBound :: Int
+-- -9223372036854775808
+--
+-- >>> minBound :: Down Int
+-- Down 9223372036854775807
+--
+-- All other instances of @'Down' a@ behave as they do for @a@.
--
-- @since 4.6.0.0
newtype Down a = Down
@@ -81,12 +100,9 @@ newtype Down a = Down
, Semigroup -- ^ @since 4.11.0.0
, Monoid -- ^ @since 4.11.0.0
, Bits -- ^ @since 4.14.0.0
- , Bounded -- ^ @since 4.14.0.0
- , Enum -- ^ @since 4.14.0.0
, FiniteBits -- ^ @since 4.14.0.0
, Floating -- ^ @since 4.14.0.0
, Fractional -- ^ @since 4.14.0.0
- , Integral -- ^ @since 4.14.0.0
, Ix -- ^ @since 4.14.0.0
, Real -- ^ @since 4.14.0.0
, RealFrac -- ^ @since 4.14.0.0
@@ -114,6 +130,13 @@ instance (Show a) => Show (Down a) where
instance Ord a => Ord (Down a) where
compare (Down x) (Down y) = y `compare` x
+-- | Swaps @'minBound'@ and @'maxBound'@ of the underlying type.
+--
+-- @since 4.14.0.0
+instance Bounded a => Bounded (Down a) where
+ minBound = Down maxBound
+ maxBound = Down minBound
+
-- | @since 4.11.0.0
instance Functor Down where
fmap = coerce
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 79e47093cc..56fedd5ed6 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -44,6 +44,9 @@
* `catMaybes` is now implemented using `mapMaybe`, so that it is both a "good
consumer" and "good producer" for list-fusion (#18574)
+ * Correct `Bounded` instance and remove `Enum` and `Integral` instances for
+ `Data.Ord.Down`.
+
## 4.14.0.0 *TBA*
* Bundled with GHC 8.10.1