summaryrefslogtreecommitdiff
path: root/libraries/base
diff options
context:
space:
mode:
authorDavid Feuer <David.Feuer@gmail.com>2021-08-22 20:55:52 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-08-27 17:38:19 -0400
commite28773fce96e5252a8addb89535feb57b5738512 (patch)
tree7b8a7f19cb04cf1176facd0d1b5f67772805eefe /libraries/base
parent14c80432766b9cde57cd87889d6e7b60fff686ff (diff)
downloadhaskell-e28773fce96e5252a8addb89535feb57b5738512.tar.gz
Export Solo from Data.Tuple
* The `Solo` type is intended to be the canonical lifted unary tuple. Up until now, it has only been available from `GHC.Tuple` in `ghc-prim`. Export it from `Data.Tuple` in `base`. I proposed this on the libraries list in December, 2020. https://mail.haskell.org/pipermail/libraries/2020-December/031061.html Responses from chessai https://mail.haskell.org/pipermail/libraries/2020-December/031062.html and George Wilson https://mail.haskell.org/pipermail/libraries/2021-January/031077.html were positive. There were no other responses. * Add Haddock documentation for Solo. * Give `Solo` a single field, `getSolo`, a custom `Show` instance that does *not* use record syntax, and a `Read` instance that accepts either record syntax or non-record syntax.
Diffstat (limited to 'libraries/base')
-rw-r--r--libraries/base/Data/Tuple.hs4
-rw-r--r--libraries/base/GHC/Read.hs20
-rw-r--r--libraries/base/GHC/Show.hs4
-rw-r--r--libraries/base/tests/all.T1
-rw-r--r--libraries/base/tests/read-show-solo.hs9
-rw-r--r--libraries/base/tests/read-show-solo.stdout5
6 files changed, 40 insertions, 3 deletions
diff --git a/libraries/base/Data/Tuple.hs b/libraries/base/Data/Tuple.hs
index 569dd14da0..5b64f17601 100644
--- a/libraries/base/Data/Tuple.hs
+++ b/libraries/base/Data/Tuple.hs
@@ -16,7 +16,8 @@
-----------------------------------------------------------------------------
module Data.Tuple
- ( fst
+ ( Solo (..)
+ , fst
, snd
, curry
, uncurry
@@ -24,6 +25,7 @@ module Data.Tuple
) where
import GHC.Base () -- Note [Depend on GHC.Tuple]
+import GHC.Tuple (Solo (..))
default () -- Double isn't available yet
diff --git a/libraries/base/GHC/Read.hs b/libraries/base/GHC/Read.hs
index 7f698ec498..43e5ee5b32 100644
--- a/libraries/base/GHC/Read.hs
+++ b/libraries/base/GHC/Read.hs
@@ -669,7 +669,25 @@ instance Read () where
readList = readListDefault
-- | @since 4.15
-deriving instance Read a => Read (Solo a)
+instance Read a => Read (Solo a) where
+ -- Since our `show` doesn't show record syntax, we want to accept non-record
+ -- syntax. Since Solo is actually a record, it only seems fair to accept
+ -- record syntax as well.
+ readPrec = parens $
+ (prec appPrec $
+ do expectP (L.Ident "Solo")
+ x <- step readPrec
+ return (Solo x))
+ +++
+ (prec appPrec1
+ (do expectP (L.Ident "Solo")
+ expectP (L.Punc "{")
+ x <- readField
+ "getSolo" (reset readPrec)
+ expectP (L.Punc "}")
+ return (Solo x)))
+
+ readListPrec = readListPrecDefault
-- | @since 2.01
instance (Read a, Read b) => Read (a,b) where
diff --git a/libraries/base/GHC/Show.hs b/libraries/base/GHC/Show.hs
index 04fbcb6112..ecfb7dbe0f 100644
--- a/libraries/base/GHC/Show.hs
+++ b/libraries/base/GHC/Show.hs
@@ -169,7 +169,9 @@ appPrec1 = I# 11# -- appPrec + 1
deriving instance Show ()
-- | @since 4.15
-deriving instance Show a => Show (Solo a)
+instance Show a => Show (Solo a) where
+ showsPrec d (Solo x) = showParen (d > 10) $
+ showString "Solo " . showsPrec 11 x
-- | @since 2.01
instance Show a => Show [a] where
diff --git a/libraries/base/tests/all.T b/libraries/base/tests/all.T
index ebbf81ec52..371ed56327 100644
--- a/libraries/base/tests/all.T
+++ b/libraries/base/tests/all.T
@@ -45,6 +45,7 @@ test('inits', normal, compile_and_run, [''])
test('genericNegative001', extra_run_opts('-1'), compile_and_run, [''])
test('ix001', normal, compile_and_run, [''])
test('isValidNatural', normal, compile_and_run, [''])
+test('read-show-solo', normal, compile_and_run, [''])
# need to add -K64m to the compiler opts, so that GHCi gets it too
test('ioref001',
diff --git a/libraries/base/tests/read-show-solo.hs b/libraries/base/tests/read-show-solo.hs
new file mode 100644
index 0000000000..553e837836
--- /dev/null
+++ b/libraries/base/tests/read-show-solo.hs
@@ -0,0 +1,9 @@
+module Main (main) where
+import Data.Tuple (Solo (..))
+
+main = do
+ print $ Solo (3 :: Int)
+ print $ Solo (Just "")
+ print $ Just (Solo "")
+ print (read (show (Solo (3 :: Int))) :: Solo Int)
+ print (read "Just Solo { getSolo = 5 }" :: Maybe (Solo Int))
diff --git a/libraries/base/tests/read-show-solo.stdout b/libraries/base/tests/read-show-solo.stdout
new file mode 100644
index 0000000000..9478a3df92
--- /dev/null
+++ b/libraries/base/tests/read-show-solo.stdout
@@ -0,0 +1,5 @@
+Solo 3
+Solo (Just "")
+Just (Solo "")
+Solo 3
+Just (Solo 5)