[Git][ghc/ghc][master] Fix `instance Bounded a => Bounded (Down a)` (#18716)
Marge Bot
gitlab at gitlab.haskell.org
Tue Oct 27 17:59:42 UTC 2020
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
9ad51bc9 by David Beacham at 2020-10-27T13:59:35-04:00
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
- - - - -
2 changed files:
- libraries/base/Data/Ord.hs
- libraries/base/changelog.md
Changes:
=====================================
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 at .
+--
+-- >>> 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 at .
--
-- @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
=====================================
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
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9ad51bc9d2ad9168abad271f715ce73d3562218a
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9ad51bc9d2ad9168abad271f715ce73d3562218a
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20201027/f9fc5cab/attachment-0001.html>
More information about the ghc-commits
mailing list