diff options
author | David Beacham <dbeacham@dbeacham.co.uk> | 2020-10-06 13:31:09 +0100 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2020-11-09 14:15:13 -0500 |
commit | e49c8923c99c658719cd5495674ae3bad985118a (patch) | |
tree | 4f9842c668f751f8d10f390fa393cbab44ff33bc /libraries | |
parent | 6c1cf2801a047e5c1ea7a92837f6c029210e1ce4 (diff) | |
download | haskell-e49c8923c99c658719cd5495674ae3bad985118a.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
(cherry picked from commit 9ad51bc9d2ad9168abad271f715ce73d3562218a)
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/base/Data/Ord.hs | 35 | ||||
-rw-r--r-- | libraries/base/changelog.md | 5 |
2 files changed, 33 insertions, 7 deletions
diff --git a/libraries/base/Data/Ord.hs b/libraries/base/Data/Ord.hs index 8703c7bdc0..1132344aa6 100644 --- a/libraries/base/Data/Ord.hs +++ b/libraries/base/Data/Ord.hs @@ -27,11 +27,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 -- | @@ -46,10 +46,29 @@ comparing p x y = compare (p x) (p y) -- | 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 @@ -61,12 +80,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 @@ -94,6 +110,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 34b1a8d1bf..749448fa8a 100644 --- a/libraries/base/changelog.md +++ b/libraries/base/changelog.md @@ -33,7 +33,10 @@ * Add `Ix` instances for tuples of size 6 through 15 -## 4.14.0.0 *Jan 2020 + * Correct `Bounded` instance and remove `Enum` and `Integral` instances for + `Data.Ord.Down`. + +## 4.14.0.0 *Jan 2020* * Bundled with GHC 8.10.1 * Add a `TestEquality` instance for the `Compose` newtype. |