[commit: ghc] master: Add instances for Data.Ord.Down (47be644)

git at git.haskell.org git at git.haskell.org
Wed Apr 26 01:12:02 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/47be6444d35783eea7dc3ab8b2f11626777cdbd8/ghc

>---------------------------------------------------------------

commit 47be6444d35783eea7dc3ab8b2f11626777cdbd8
Author: Adam Sandberg Eriksson <adam at sandbergericsson.se>
Date:   Tue Apr 25 18:41:28 2017 -0400

    Add instances for Data.Ord.Down
    
    Namely `Num`, `Functor`, `Applicative`, `Monad`, `Semigroup` and
    `Monoid` for `Data.Ord.Down` (#13097).
    
    Reviewers: austin, hvr, bgamari, RyanGlScott
    
    Reviewed By: bgamari, RyanGlScott
    
    Subscribers: RyanGlScott, rwbarton, thomie
    
    GHC Trac Issues: #13097
    
    Differential Revision: https://phabricator.haskell.org/D3500


>---------------------------------------------------------------

47be6444d35783eea7dc3ab8b2f11626777cdbd8
 libraries/base/Data/Ord.hs                         | 26 +++++++++++++++++++---
 libraries/base/Data/Semigroup.hs                   |  6 +++++
 libraries/base/changelog.md                        |  4 ++++
 libraries/base/tests/T13097.hs                     |  7 ++++++
 .../base/tests/T13097.stdout                       |  0
 libraries/base/tests/all.T                         |  1 +
 6 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/libraries/base/Data/Ord.hs b/libraries/base/Data/Ord.hs
index 767d7b3..11d6967 100644
--- a/libraries/base/Data/Ord.hs
+++ b/libraries/base/Data/Ord.hs
@@ -1,5 +1,6 @@
 {-# LANGUAGE Trustworthy #-}
 {-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
 
 -----------------------------------------------------------------------------
 -- |
@@ -25,6 +26,7 @@ module Data.Ord (
 import GHC.Base
 import GHC.Show
 import GHC.Read
+import GHC.Num
 
 -- | 
 -- > comparing p x y = compare (p x) (p y)
@@ -43,11 +45,29 @@ comparing p x y = compare (p x) (p y)
 -- This is particularly useful when sorting in generalised list comprehensions,
 -- as in: @then sortWith by 'Down' x@
 --
--- Provides 'Show' and 'Read' instances (/since: 4.7.0.0/).
---
 -- @since 4.6.0.0
-newtype Down a = Down a deriving (Eq, Show, Read)
+newtype Down a = Down a
+    deriving
+      ( Eq
+      , Show -- ^ @since 4.7.0.0
+      , Read -- ^ @since 4.7.0.0
+      , Num -- ^ @since 4.11.0.0
+      , Monoid -- ^ @since 4.11.0.0
+      )
 
 -- | @since 4.6.0.0
 instance Ord a => Ord (Down a) where
     compare (Down x) (Down y) = y `compare` x
+
+-- | @since 4.11.0.0
+instance Functor Down where
+    fmap = coerce
+
+-- | @since 4.11.0.0
+instance Applicative Down where
+    pure = Down
+    (<*>) = coerce
+
+-- | @since 4.11.0.0
+instance Monad Down where
+    Down a >>= k = k a
diff --git a/libraries/base/Data/Semigroup.hs b/libraries/base/Data/Semigroup.hs
index e6bc314..ec68683 100644
--- a/libraries/base/Data/Semigroup.hs
+++ b/libraries/base/Data/Semigroup.hs
@@ -83,6 +83,7 @@ import           Data.Monoid         (All (..), Any (..), Dual (..), Endo (..),
                                       Product (..), Sum (..))
 import           Data.Monoid         (Alt (..))
 import qualified Data.Monoid         as Monoid
+import           Data.Ord            (Down(..))
 import           Data.Void
 #ifndef mingw32_HOST_OS
 import           GHC.Event           (Event, Lifetime)
@@ -238,6 +239,11 @@ instance Semigroup Any where
   (<>) = coerce (||)
   stimes = stimesIdempotentMonoid
 
+-- | @since 4.11.0.0
+instance Semigroup a => Semigroup (Down a) where
+  Down a <> Down b = Down (a <> b)
+  stimes n (Down a) = Down (stimes n a)
+
 
 -- | @since 4.9.0.0
 instance Num a => Semigroup (Sum a) where
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index e2e276a..69baab3 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -5,6 +5,10 @@
 
   * Add `Alternative` instance for `ZipList` (#13520)
 
+  * Add instances `Num`, `Functor`, `Applicative`, `Monad`, `Semigroup`
+    and `Monoid` for `Data.Ord.Down` (#13097).
+
+
 ## 4.10.0.0 *April 2017*
   * Bundled with GHC *TBA*
 
diff --git a/libraries/base/tests/T13097.hs b/libraries/base/tests/T13097.hs
new file mode 100644
index 0000000..f51b7cf
--- /dev/null
+++ b/libraries/base/tests/T13097.hs
@@ -0,0 +1,7 @@
+{-# LANGUAGE TypeApplications #-}
+import Data.Ord
+
+main :: IO ()
+main = do
+  print ((<) 10 20)
+  print ((<) @(Down _) 10 20)
diff --git a/testsuite/tests/driver/recomp008/recomp008.stdout b/libraries/base/tests/T13097.stdout
similarity index 100%
copy from testsuite/tests/driver/recomp008/recomp008.stdout
copy to libraries/base/tests/T13097.stdout
diff --git a/libraries/base/tests/all.T b/libraries/base/tests/all.T
index 33055f3..4bd8084 100644
--- a/libraries/base/tests/all.T
+++ b/libraries/base/tests/all.T
@@ -213,3 +213,4 @@ test('T13191',
       compile_and_run,
       ['-O'])
 test('T13525', when(opsys('mingw32'), skip), compile_and_run, [''])
+test('T13097', normal, compile_and_run, [''])



More information about the ghc-commits mailing list