summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph C. Sible <3439-josephcsible@users.noreply.gitlab.haskell.org>2020-04-26 13:47:40 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-05-08 15:29:40 -0400
commitbb35c0e573b0bc8fe6a28c00734fa124da02a755 (patch)
tree8a911cb0326bb096b19113234aedbcfe14e51214
parent09ac8de5777d5ca953279a6c0ee42a6fba0fcba6 (diff)
downloadhaskell-bb35c0e573b0bc8fe6a28c00734fa124da02a755.tar.gz
Document lawlessness of Ap's Num instance
-rw-r--r--libraries/base/Data/Monoid.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/libraries/base/Data/Monoid.hs b/libraries/base/Data/Monoid.hs
index cf55b2150c..413072262b 100644
--- a/libraries/base/Data/Monoid.hs
+++ b/libraries/base/Data/Monoid.hs
@@ -233,6 +233,32 @@ instance (Applicative f, Bounded a) => Bounded (Ap f a) where
minBound = pure minBound
maxBound = pure maxBound
+-- Note that even if the underlying 'Num' and 'Applicative' instances are
+-- lawful, for most 'Applicative's, this instance will not be lawful. It's
+-- particularly tempting to use this with lists, but then these laws all fail
+-- to hold:
+--
+-- Commutativity:
+--
+-- >>> Ap [10,20] + Ap [1,2]
+-- Ap {getAp = [11,12,21,22]}
+-- >>> Ap [1,2] + Ap [10,20]
+-- Ap {getAp = [11,21,12,22]}
+--
+-- Additive inverse:
+--
+-- >>> Ap [] + negate (Ap [])
+-- Ap {getAp = []}
+-- >>> fromInteger 0 :: Ap [] Int
+-- Ap {getAp = [0]}
+--
+-- Distributivity:
+--
+-- >>> Ap [1,2] * (3 + 4)
+-- Ap {getAp = [7,14]}
+-- >>> (Ap [1,2] * 3) + (Ap [1,2] * 4)
+-- Ap {getAp = [7,11,10,14]}
+--
-- | @since 4.12.0.0
instance (Applicative f, Num a) => Num (Ap f a) where
(+) = liftA2 (+)