summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Feuer <David.Feuer@gmail.com>2021-08-31 03:42:30 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-09-18 12:01:10 -0400
commit7bc16521b93f9db62190dd4397a836e101932b8c (patch)
treeb8efd9e0ca0eba008d3c4b4e46b0dbdde76eb755
parent20e6fec8106521efe7c571612d6d4353fec5c832 (diff)
downloadhaskell-7bc16521b93f9db62190dd4397a836e101932b8c.tar.gz
Add more instances for Solo
Oleg Grenrus pointed out that `Solo` was missing `Eq`, `Ord`, `Bounded`, `Enum`, and `Ix` instances, which were all apparently available for the `OneTuple` type (in the `OneTuple` package). Though only the first three really seem useful, there's no reason not to take them all. For `Ix`, `Solo` naturally fills a gap between `()` and `(,)`.
-rw-r--r--libraries/base/GHC/Enum.hs17
-rw-r--r--libraries/base/GHC/Ix.hs18
-rw-r--r--libraries/ghc-prim/GHC/Classes.hs2
-rw-r--r--testsuite/tests/ado/T13242a.stderr2
-rw-r--r--testsuite/tests/partial-sigs/should_fail/T10999.stderr2
-rw-r--r--testsuite/tests/typecheck/should_fail/T5095.stderr4
-rw-r--r--testsuite/tests/typecheck/should_fail/tcfail072.stderr2
7 files changed, 42 insertions, 5 deletions
diff --git a/libraries/base/GHC/Enum.hs b/libraries/base/GHC/Enum.hs
index 2765dcd265..096e633894 100644
--- a/libraries/base/GHC/Enum.hs
+++ b/libraries/base/GHC/Enum.hs
@@ -36,6 +36,7 @@ import GHC.Char
import GHC.Num.Integer
import GHC.Num
import GHC.Show
+import GHC.Tuple (Solo (..))
default () -- Double isn't available yet
-- | The 'Bounded' class is used to name the upper and lower limits of a
@@ -234,6 +235,22 @@ instance Enum () where
enumFromTo () () = [()]
enumFromThenTo () () () = let many = ():many in many
+instance Enum a => Enum (Solo a) where
+ succ (Solo a) = Solo (succ a)
+ pred (Solo a) = Solo (pred a)
+
+ toEnum x = Solo (toEnum x)
+
+ fromEnum (Solo x) = fromEnum x
+ enumFrom (Solo x) = [Solo a | a <- enumFrom x]
+ enumFromThen (Solo x) (Solo y) =
+ [Solo a | a <- enumFromThen x y]
+ enumFromTo (Solo x) (Solo y) =
+ [Solo a | a <- enumFromTo x y]
+ enumFromThenTo (Solo x) (Solo y) (Solo z) =
+ [Solo a | a <- enumFromThenTo x y z]
+
+deriving instance Bounded a => Bounded (Solo a)
-- Report requires instances up to 15
-- | @since 2.01
deriving instance (Bounded a, Bounded b)
diff --git a/libraries/base/GHC/Ix.hs b/libraries/base/GHC/Ix.hs
index 34c4857b15..b523fe2b4f 100644
--- a/libraries/base/GHC/Ix.hs
+++ b/libraries/base/GHC/Ix.hs
@@ -26,6 +26,7 @@ import GHC.Num
import GHC.Base
import GHC.Real( fromIntegral )
import GHC.Show
+import GHC.Tuple (Solo (..))
-- | The 'Ix' class is used to map a contiguous subrange of values in
-- a type onto integers. It is used primarily for array indexing
@@ -266,6 +267,23 @@ instance Ix () where
{-# INLINE index #-} -- See Note [Inlining index]
index b i = unsafeIndex b i
+instance Ix a => Ix (Solo a) where -- as derived
+ {-# SPECIALISE instance Ix (Solo Int) #-}
+
+ {-# INLINE range #-}
+ range (Solo l, Solo u) =
+ [ Solo i | i <- range (l,u) ]
+
+ {-# INLINE unsafeIndex #-}
+ unsafeIndex (Solo l, Solo u) (Solo i) =
+ unsafeIndex (l,u) i
+
+ {-# INLINE inRange #-}
+ inRange (Solo l, Solo u) (Solo i) =
+ inRange (l, u) i
+
+ -- Default method for index
+
----------------------------------------------------------------------
-- | @since 2.01
instance (Ix a, Ix b) => Ix (a, b) where -- as derived
diff --git a/libraries/ghc-prim/GHC/Classes.hs b/libraries/ghc-prim/GHC/Classes.hs
index fb9ea98581..2c874fb241 100644
--- a/libraries/ghc-prim/GHC/Classes.hs
+++ b/libraries/ghc-prim/GHC/Classes.hs
@@ -149,6 +149,7 @@ class Eq a where
{-# MINIMAL (==) | (/=) #-}
deriving instance Eq ()
+deriving instance Eq a => Eq (Solo a)
deriving instance (Eq a, Eq b) => Eq (a, b)
deriving instance (Eq a, Eq b, Eq c) => Eq (a, b, c)
deriving instance (Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d)
@@ -359,6 +360,7 @@ class (Eq a) => Ord a where
{-# MINIMAL compare | (<=) #-}
deriving instance Ord ()
+deriving instance Ord a => Ord (Solo a)
deriving instance (Ord a, Ord b) => Ord (a, b)
deriving instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
deriving instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
diff --git a/testsuite/tests/ado/T13242a.stderr b/testsuite/tests/ado/T13242a.stderr
index eb2bf062ca..f5dce341ac 100644
--- a/testsuite/tests/ado/T13242a.stderr
+++ b/testsuite/tests/ado/T13242a.stderr
@@ -29,7 +29,7 @@ T13242a.hs:13:13: error:
instance Eq Ordering -- Defined in ‘GHC.Classes’
instance Eq Integer -- Defined in ‘GHC.Num.Integer’
instance Eq () -- Defined in ‘GHC.Classes’
- ...plus 21 others
+ ...plus 22 others
...plus five instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: return (x == x)
diff --git a/testsuite/tests/partial-sigs/should_fail/T10999.stderr b/testsuite/tests/partial-sigs/should_fail/T10999.stderr
index 356b068031..71bab83508 100644
--- a/testsuite/tests/partial-sigs/should_fail/T10999.stderr
+++ b/testsuite/tests/partial-sigs/should_fail/T10999.stderr
@@ -25,7 +25,7 @@ T10999.hs:8:28: error:
instance Ord a => Ord (Set.Set a) -- Defined in ‘Data.Set.Internal’
instance Ord Ordering -- Defined in ‘GHC.Classes’
instance Ord Integer -- Defined in ‘GHC.Num.Integer’
- ...plus 22 others
+ ...plus 23 others
...plus two instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘($)’, namely ‘f ()’
diff --git a/testsuite/tests/typecheck/should_fail/T5095.stderr b/testsuite/tests/typecheck/should_fail/T5095.stderr
index 0bfbc41c80..e30898f74f 100644
--- a/testsuite/tests/typecheck/should_fail/T5095.stderr
+++ b/testsuite/tests/typecheck/should_fail/T5095.stderr
@@ -5,8 +5,8 @@ T5095.hs:9:11: error:
instance [overlappable] Show a => Eq a -- Defined at T5095.hs:5:31
instance Eq Ordering -- Defined in ‘GHC.Classes’
instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Maybe’
- ...plus 23 others
- ...plus 7 instances involving out-of-scope types
+ ...plus 24 others
+ ...plus six instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
(The choice depends on the instantiation of ‘a’
To pick the first instance above, use IncoherentInstances
diff --git a/testsuite/tests/typecheck/should_fail/tcfail072.stderr b/testsuite/tests/typecheck/should_fail/tcfail072.stderr
index 0b69f4ef63..b91f96bf37 100644
--- a/testsuite/tests/typecheck/should_fail/tcfail072.stderr
+++ b/testsuite/tests/typecheck/should_fail/tcfail072.stderr
@@ -10,7 +10,7 @@ tcfail072.hs:23:13: error:
instance Ord Ordering -- Defined in ‘GHC.Classes’
instance Ord Integer -- Defined in ‘GHC.Num.Integer’
instance Ord () -- Defined in ‘GHC.Classes’
- ...plus 21 others
+ ...plus 22 others
...plus two instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: g A