summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorFumiaki Kinoshita <fumiexcel@gmail.com>2015-01-14 20:41:30 +0900
committerHerbert Valerio Riedel <hvr@gnu.org>2015-01-14 20:52:06 +0100
commitc71fb84b8c9ec9c1e279df8c75ceb8a537801aa1 (patch)
treed1943a161795fcfc8b8a918b29d2ff2d6e7479a6 /libraries
parentc823b73cb2ca8e2392e2a4c48286879cc7baa51c (diff)
downloadhaskell-c71fb84b8c9ec9c1e279df8c75ceb8a537801aa1.tar.gz
Add Eq, Ord, Show, and Read instances for Const
As suggested in https://www.haskell.org/pipermail/libraries/2013-October/021531.html this adds the following instances - `Show a => Show (Const a b)` - `Read a => Read (Const a b)` - `Eq a => Eq (Const a b)` - `Ord a => Ord (Const a b)` The Read/Show instances are defined in such a way as if `Const` was defined without record-syntax (i.e. as `newtype Const a b = Const a`) Addresses #9984 Reviewed By: ekmett Differential Revision: https://phabricator.haskell.org/D619
Diffstat (limited to 'libraries')
-rw-r--r--libraries/base/Control/Applicative.hs14
-rw-r--r--libraries/base/changelog.md3
2 files changed, 13 insertions, 4 deletions
diff --git a/libraries/base/Control/Applicative.hs b/libraries/base/Control/Applicative.hs
index a0627e440d..02062e2e19 100644
--- a/libraries/base/Control/Applicative.hs
+++ b/libraries/base/Control/Applicative.hs
@@ -61,11 +61,19 @@ import Data.Functor ((<$>))
import GHC.Base
import GHC.Generics
import GHC.List (repeat, zipWith)
-import GHC.Read (Read)
-import GHC.Show (Show)
+import GHC.Read (Read(readsPrec), readParen, lex)
+import GHC.Show (Show(showsPrec), showParen, showString)
newtype Const a b = Const { getConst :: a }
- deriving (Generic, Generic1, Monoid)
+ deriving (Generic, Generic1, Monoid, Eq, Ord)
+
+instance Read a => Read (Const a b) where
+ readsPrec d = readParen (d > 10)
+ $ \r -> [(Const x,t) | ("Const", s) <- lex r, (x, t) <- readsPrec 11 s]
+
+instance Show a => Show (Const a b) where
+ showsPrec d (Const x) = showParen (d > 10) $
+ showString "Const " . showsPrec 11 x
instance Foldable (Const m) where
foldMap _ _ = mempty
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 76a6a191dd..83ae5e44c1 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -176,7 +176,8 @@
* There are now `Foldable` and `Traversable` instances for `Either a`,
`Const r`, and `(,) a`.
- * There is now a `Monoid`, `Generic`, and `Generic1` instance for `Const`.
+ * There are now `Show`, `Read`, `Eq`, `Ord`, `Monoid`, `Generic`, and
+ `Generic1` instances for `Const`.
* There is now a `Data` instance for `Data.Version`.