summaryrefslogtreecommitdiff
path: root/libraries/base/Data/Foldable.hs
diff options
context:
space:
mode:
authorOleg Grenrus <oleg.grenrus@iki.fi>2015-03-03 07:21:43 -0600
committerAustin Seipp <austin@well-typed.com>2015-03-03 07:21:44 -0600
commit4e6bcc2c8134f9c1ba7d715b3206130f23c529fb (patch)
treee96cb726189a973f1e25982cc2c0d64bd3b4a8f1 /libraries/base/Data/Foldable.hs
parent89458eba5721de1b6b3378415f26e110bab8cc0f (diff)
downloadhaskell-4e6bcc2c8134f9c1ba7d715b3206130f23c529fb.tar.gz
Add various instances to newtypes in Data.Monoid
Summary: Add Functor instances for Dual, Sum and Product Add Foldable instances for Dual, Sum and Product Add Traversable instances for Dual, Sum and Product Add Foldable and Traversable instances for First and Last Add Applicative, Monad instances to Dual, Sum, Product Add MonadFix to Data.Monoid wrappers Derive Data for Identity Add Data instances to Data.Monoid wrappers Add Data (Alt f a) instance Reviewers: ekmett, dfeuer, hvr, austin Reviewed By: dfeuer, austin Subscribers: thomie Differential Revision: https://phabricator.haskell.org/D673 GHC Trac Issues: #10107
Diffstat (limited to 'libraries/base/Data/Foldable.hs')
-rw-r--r--libraries/base/Data/Foldable.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs
index a745f66092..1f20261943 100644
--- a/libraries/base/Data/Foldable.hs
+++ b/libraries/base/Data/Foldable.hs
@@ -282,6 +282,66 @@ instance Foldable Proxy where
sum _ = 0
product _ = 1
+instance Foldable Dual where
+ foldMap = coerce
+
+ elem = (. getDual) #. (==)
+ foldl = coerce
+ foldl' = coerce
+ foldl1 _ = getDual
+ foldr f z (Dual x) = f x z
+ foldr' = foldr
+ foldr1 _ = getDual
+ length _ = 1
+ maximum = getDual
+ minimum = getDual
+ null _ = False
+ product = getDual
+ sum = getDual
+ toList (Dual x) = [x]
+
+instance Foldable Sum where
+ foldMap = coerce
+
+ elem = (. getSum) #. (==)
+ foldl = coerce
+ foldl' = coerce
+ foldl1 _ = getSum
+ foldr f z (Sum x) = f x z
+ foldr' = foldr
+ foldr1 _ = getSum
+ length _ = 1
+ maximum = getSum
+ minimum = getSum
+ null _ = False
+ product = getSum
+ sum = getSum
+ toList (Sum x) = [x]
+
+instance Foldable Product where
+ foldMap = coerce
+
+ elem = (. getProduct) #. (==)
+ foldl = coerce
+ foldl' = coerce
+ foldl1 _ = getProduct
+ foldr f z (Product x) = f x z
+ foldr' = foldr
+ foldr1 _ = getProduct
+ length _ = 1
+ maximum = getProduct
+ minimum = getProduct
+ null _ = False
+ product = getProduct
+ sum = getProduct
+ toList (Product x) = [x]
+
+instance Foldable First where
+ foldMap f = foldMap f . getFirst
+
+instance Foldable Last where
+ foldMap f = foldMap f . getLast
+
-- We don't export Max and Min because, as Edward Kmett pointed out to me,
-- there are two reasonable ways to define them. One way is to use Maybe, as we
-- do here; the other way is to impose a Bounded constraint on the Monoid