[commit: ghc] master: Add Monad instance for `((, ) a)` (#10190) (9db005a)
git at git.haskell.org
git at git.haskell.org
Wed Mar 25 13:51:25 UTC 2015
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/9db005a444722e31aca1956b058e069bcf3cacbd/ghc
>---------------------------------------------------------------
commit 9db005a444722e31aca1956b058e069bcf3cacbd
Author: Fumiaki Kinoshita <fumiexcel at gmail.com>
Date: Wed Mar 25 13:30:25 2015 +0900
Add Monad instance for `((,) a)` (#10190)
This was proposed a couple of times in the past, e.g.
- https://mail.haskell.org/pipermail/libraries/2011-November/017153.html
- https://mail.haskell.org/pipermail/libraries/2013-July/020446.html
but its implementation had been blocked by the fact that `Monoid` wasn't
in scope where the `Monad` class was defined. Since the AMP/FTP restructuring
this is no longer the case.
>---------------------------------------------------------------
9db005a444722e31aca1956b058e069bcf3cacbd
libraries/base/GHC/Base.hs | 3 +++
libraries/base/changelog.md | 2 ++
testsuite/tests/ghci/scripts/T7627.stdout | 1 +
testsuite/tests/ghci/scripts/ghci011.stdout | 1 +
testsuite/tests/typecheck/should_fail/tcfail181.stderr | 2 +-
5 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/libraries/base/GHC/Base.hs b/libraries/base/GHC/Base.hs
index 7e04ab4..d9192d7 100644
--- a/libraries/base/GHC/Base.hs
+++ b/libraries/base/GHC/Base.hs
@@ -308,6 +308,9 @@ instance Monoid a => Applicative ((,) a) where
pure x = (mempty, x)
(u, f) <*> (v, x) = (u `mappend` v, f x)
+instance Monoid a => Monad ((,) a) where
+ return x = (mempty, x)
+ (u, a) >>= k = case k a of (v, b) -> (u `mappend` v, b)
{- | The 'Functor' class is used for types that can be mapped over.
Instances of 'Functor' should satisfy the following laws:
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 849a7ad..f402189 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -17,6 +17,8 @@
* `Dual`, `Product`, and `Sum` now have `Functor`, `Applicative`, and
`Monad` instances
+ * `(,) a` now has a `Monad` instance
+
* Redundant typeclass constraints have been removed:
- `Data.Ratio.{denominator,numerator}` have no `Integral` constraint anymore
- **TODO**
diff --git a/testsuite/tests/ghci/scripts/T7627.stdout b/testsuite/tests/ghci/scripts/T7627.stdout
index 2713566..158672c 100644
--- a/testsuite/tests/ghci/scripts/T7627.stdout
+++ b/testsuite/tests/ghci/scripts/T7627.stdout
@@ -15,6 +15,7 @@ data (,) a b = (,) a b -- Defined in ‘GHC.Tuple’
instance (Bounded a, Bounded b) => Bounded (a, b)
-- Defined in ‘GHC.Enum’
instance (Eq a, Eq b) => Eq (a, b) -- Defined in ‘GHC.Classes’
+instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’
instance Functor ((,) a) -- Defined in ‘GHC.Base’
instance (Ord a, Ord b) => Ord (a, b) -- Defined in ‘GHC.Classes’
instance (Read a, Read b) => Read (a, b) -- Defined in ‘GHC.Read’
diff --git a/testsuite/tests/ghci/scripts/ghci011.stdout b/testsuite/tests/ghci/scripts/ghci011.stdout
index a608f07..8042757 100644
--- a/testsuite/tests/ghci/scripts/ghci011.stdout
+++ b/testsuite/tests/ghci/scripts/ghci011.stdout
@@ -21,6 +21,7 @@ data (,) a b = (,) a b -- Defined in ‘GHC.Tuple’
instance (Bounded a, Bounded b) => Bounded (a, b)
-- Defined in ‘GHC.Enum’
instance (Eq a, Eq b) => Eq (a, b) -- Defined in ‘GHC.Classes’
+instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’
instance Functor ((,) a) -- Defined in ‘GHC.Base’
instance (Ord a, Ord b) => Ord (a, b) -- Defined in ‘GHC.Classes’
instance (Read a, Read b) => Read (a, b) -- Defined in ‘GHC.Read’
diff --git a/testsuite/tests/typecheck/should_fail/tcfail181.stderr b/testsuite/tests/typecheck/should_fail/tcfail181.stderr
index e638099..787b62e 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:
instance Monad Maybe -- Defined in ‘GHC.Base’
instance Monad IO -- Defined in ‘GHC.Base’
instance Monad ((->) r) -- Defined in ‘GHC.Base’
- ...plus one other
+ ...plus two others
In the expression: foo
In the expression: foo {bar = return True}
In an equation for ‘wog’: wog x = foo {bar = return True}
More information about the ghc-commits
mailing list