diff options
author | Fumiaki Kinoshita <fumiexcel@gmail.com> | 2019-04-07 17:44:20 +0900 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2019-10-04 21:43:49 -0400 |
commit | ec93d2a90a2d4f189feafd21575b9e9ba5ba9a5d (patch) | |
tree | 392714d5c8508fb34e69967cdc428d047cb4d3ab | |
parent | 1357d02380641ba33b05eb87c80e6a4250cd4a3b (diff) | |
download | haskell-ec93d2a90a2d4f189feafd21575b9e9ba5ba9a5d.tar.gz |
Add Monad instances to `(,,) a b` and `(,,,) a b c`
-rw-r--r-- | libraries/base/Data/Foldable.hs | 2 | ||||
-rw-r--r-- | libraries/base/GHC/Base.hs | 26 | ||||
-rw-r--r-- | libraries/base/changelog.md | 3 | ||||
-rw-r--r-- | testsuite/tests/ghci/scripts/T12550.stdout | 2 | ||||
-rw-r--r-- | testsuite/tests/partial-sigs/should_compile/T10403.stderr | 2 | ||||
-rw-r--r-- | testsuite/tests/typecheck/should_fail/T13292.stderr | 2 | ||||
-rw-r--r-- | testsuite/tests/typecheck/should_fail/tcfail181.stderr | 2 |
7 files changed, 36 insertions, 3 deletions
diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs index c17de972af..82bb3d9077 100644 --- a/libraries/base/Data/Foldable.hs +++ b/libraries/base/Data/Foldable.hs @@ -364,6 +364,8 @@ instance Foldable ((,) a) where foldMap f (_, y) = f y foldr f z (_, y) = f y z + length _ = 1 + null _ = False -- | @since 4.8.0.0 instance Foldable (Array i) where diff --git a/libraries/base/GHC/Base.hs b/libraries/base/GHC/Base.hs index d63f5d1a86..54c6f91280 100644 --- a/libraries/base/GHC/Base.hs +++ b/libraries/base/GHC/Base.hs @@ -437,6 +437,32 @@ instance Monoid a => Applicative ((,) a) where instance Monoid a => Monad ((,) a) where (u, a) >>= k = case k a of (v, b) -> (u <> v, b) +-- | @since 4.14.0.0 +instance Functor ((,,) a b) where + fmap f (a, b, c) = (a, b, f c) + +-- | @since 4.14.0.0 +instance (Monoid a, Monoid b) => Applicative ((,,) a b) where + pure x = (mempty, mempty, x) + (a, b, f) <*> (a', b', x) = (a <> a', b <> b', f x) + +-- | @since 4.14.0.0 +instance (Monoid a, Monoid b) => Monad ((,,) a b) where + (u, v, a) >>= k = case k a of (u', v', b) -> (u <> u', v <> v', b) + +-- | @since 4.14.0.0 +instance Functor ((,,,) a b c) where + fmap f (a, b, c, d) = (a, b, c, f d) + +-- | @since 4.14.0.0 +instance (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) where + pure x = (mempty, mempty, mempty, x) + (a, b, c, f) <*> (a', b', c', x) = (a <> a', b <> b', c <> c', f x) + +-- | @since 4.14.0.0 +instance (Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) where + (u, v, w, a) >>= k = case k a of (u', v', w', b) -> (u <> u', v <> v', w <> w', b) + -- | @since 4.10.0.0 instance Semigroup a => Semigroup (IO a) where (<>) = liftA2 (<>) diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md index b97ed78ced..eeed94327e 100644 --- a/libraries/base/changelog.md +++ b/libraries/base/changelog.md @@ -36,6 +36,9 @@ * Add newtypes for `CSocklen` (`socklen_t`) and `CNfds` (`nfds_t`) to `System.Posix.Types`. + * Add `Functor`, `Applicative` and `Monad` instances to `(,,) a b` + and `(,,,) a b c` + ## 4.13.0.0 *TBA* * Bundled with GHC *TBA* diff --git a/testsuite/tests/ghci/scripts/T12550.stdout b/testsuite/tests/ghci/scripts/T12550.stdout index 81be552e5c..a3117d02c2 100644 --- a/testsuite/tests/ghci/scripts/T12550.stdout +++ b/testsuite/tests/ghci/scripts/T12550.stdout @@ -53,6 +53,8 @@ instance Functor [] -- Defined in ‘GHC.Base’ instance Functor Maybe -- Defined in ‘GHC.Base’ instance Functor IO -- Defined in ‘GHC.Base’ instance ∀ r. Functor ((->) r) -- Defined in ‘GHC.Base’ +instance ∀ a b c. Functor ((,,,) a b c) -- Defined in ‘GHC.Base’ +instance ∀ a b. Functor ((,,) a b) -- Defined in ‘GHC.Base’ instance ∀ a. Functor ((,) a) -- Defined in ‘GHC.Base’ datatypeName ∷ ∀ {d} {t ∷ ★ → (★ → ★) → ★ → ★} {f ∷ ★ → ★} {a}. diff --git a/testsuite/tests/partial-sigs/should_compile/T10403.stderr b/testsuite/tests/partial-sigs/should_compile/T10403.stderr index 870a72ed5a..fcc5e38e87 100644 --- a/testsuite/tests/partial-sigs/should_compile/T10403.stderr +++ b/testsuite/tests/partial-sigs/should_compile/T10403.stderr @@ -32,7 +32,7 @@ T10403.hs:22:15: warning: [-Wdeferred-type-errors (in -Wdefault)] instance Functor IO -- Defined in ‘GHC.Base’ instance Functor (B t) -- Defined at T10403.hs:10:10 instance Functor I -- Defined at T10403.hs:6:10 - ...plus three others + ...plus five others ...plus two instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the second argument of ‘(.)’, namely ‘fmap (const ())’ diff --git a/testsuite/tests/typecheck/should_fail/T13292.stderr b/testsuite/tests/typecheck/should_fail/T13292.stderr index 9f70b1df27..adb2738e69 100644 --- a/testsuite/tests/typecheck/should_fail/T13292.stderr +++ b/testsuite/tests/typecheck/should_fail/T13292.stderr @@ -9,7 +9,7 @@ T13292a.hs:4:12: warning: [-Wdeferred-type-errors (in -Wdefault)] instance Monad IO -- Defined in ‘GHC.Base’ instance Monad Maybe -- Defined in ‘GHC.Base’ instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’ - ...plus one other + ...plus three others ...plus two instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the expression: return () diff --git a/testsuite/tests/typecheck/should_fail/tcfail181.stderr b/testsuite/tests/typecheck/should_fail/tcfail181.stderr index 9cbc04b3d0..9f5252539b 100644 --- a/testsuite/tests/typecheck/should_fail/tcfail181.stderr +++ b/testsuite/tests/typecheck/should_fail/tcfail181.stderr @@ -10,7 +10,7 @@ tcfail181.hs:17:9: error: instance Monad IO -- Defined in ‘GHC.Base’ instance Monad Maybe -- Defined in ‘GHC.Base’ instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’ - ...plus one other + ...plus three others ...plus two instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the expression: foo |