summaryrefslogtreecommitdiff
path: root/libraries/base/Control
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/base/Control
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/base/Control')
-rw-r--r--libraries/base/Control/Applicative.hs14
1 files changed, 11 insertions, 3 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