Commit 47be6444 authored by Adam Sandberg Eriksson's avatar Adam Sandberg Eriksson 🐈 Committed by Ben Gamari

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
parent 24460269
{-# 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
......@@ -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
......
......@@ -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*
......
{-# LANGUAGE TypeApplications #-}
import Data.Ord
main :: IO ()
main = do
print ((<) 10 20)
print ((<) @(Down _) 10 20)
......@@ -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, [''])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment