Commit b67e8a3f authored by chessai's avatar chessai Committed by Ben Gamari

base: Introduce Data.Monoid.Ap

This data type witnesses the lifting of a monoid into an applicative
pointwise.
parent 6a9b9b43
......@@ -28,7 +28,7 @@ import Data.Either
import Data.Function ( fix )
import Data.Maybe
import Data.Monoid ( Dual(..), Sum(..), Product(..)
, First(..), Last(..), Alt(..) )
, First(..), Last(..), Alt(..), Ap(..) )
import GHC.Base ( Monad, NonEmpty(..), errorWithoutStackTrace, (.) )
import GHC.Generics
import GHC.List ( head, tail )
......@@ -126,6 +126,10 @@ instance MonadFix Last where
instance MonadFix f => MonadFix (Alt f) where
mfix f = Alt (mfix (getAlt . f))
-- | @since 4.12.0.0
instance MonadFix f => MonadFix (Ap f) where
mfix f = Ap (mfix (getAp . f))
-- Instances for GHC.Generics
-- | @since 4.9.0.0
instance MonadFix Par1 where
......
......@@ -1283,6 +1283,9 @@ deriving instance Data a => Data (Last a)
-- | @since 4.8.0.0
deriving instance (Data (f a), Data a, Typeable f) => Data (Alt f a)
-- | @since 4.12.0.0
deriving instance (Data (f a), Data a, Typeable f) => Data (Ap f a)
----------------------------------------------------------------------------
-- Data instances for GHC.Generics representations
......
......@@ -440,6 +440,10 @@ instance Foldable Last where
instance (Foldable f) => Foldable (Alt f) where
foldMap f = foldMap f . getAlt
-- | @since 4.12.0.0
instance (Foldable f) => Foldable (Ap f) where
foldMap f = foldMap f . getAp
-- Instances for GHC.Generics
-- | @since 4.9.0.0
instance Foldable U1 where
......
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
-----------------------------------------------------------------------------
-- |
......@@ -38,14 +38,20 @@ module Data.Monoid (
First(..),
Last(..),
-- * 'Alternative' wrapper
Alt (..)
Alt(..),
-- * 'Applicative' wrapper
Ap(..)
) where
-- Push down the module in the dependency hierarchy.
import GHC.Base hiding (Any)
import GHC.Enum
import GHC.Generics
import GHC.Num
import GHC.Read
import GHC.Show
import GHC.Generics
import Control.Monad.Fail (MonadFail)
import Data.Semigroup.Internal
......@@ -159,7 +165,47 @@ instance Semigroup (Last a) where
instance Monoid (Last a) where
mempty = Last Nothing
-- | This data type witnesses the lifting of a 'Monoid' into an
-- 'Applicative' pointwise.
--
-- @since 4.12.0.0
newtype Ap f a = Ap { getAp :: f a }
deriving ( Alternative -- ^ @since 4.12.0.0
, Applicative -- ^ @since 4.12.0.0
, Enum -- ^ @since 4.12.0.0
, Eq -- ^ @since 4.12.0.0
, Functor -- ^ @since 4.12.0.0
, Generic -- ^ @since 4.12.0.0
, Generic1 -- ^ @since 4.12.0.0
, Monad -- ^ @since 4.12.0.0
, MonadFail -- ^ @since 4.12.0.0
, MonadPlus -- ^ @since 4.12.0.0
, Ord -- ^ @since 4.12.0.0
, Read -- ^ @since 4.12.0.0
, Show -- ^ @since 4.12.0.0
)
-- | @since 4.12.0.0
instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where
(Ap x) <> (Ap y) = Ap $ liftA2 (<>) x y
-- | @since 4.12.0.0
instance (Applicative f, Monoid a) => Monoid (Ap f a) where
mempty = Ap $ pure mempty
-- | @since 4.12.0.0
instance (Applicative f, Bounded a) => Bounded (Ap f a) where
minBound = pure minBound
maxBound = pure maxBound
-- | @since 4.12.0.0
instance (Applicative f, Num a) => Num (Ap f a) where
(+) = liftA2 (+)
(*) = liftA2 (*)
negate = fmap negate
fromInteger = pure . fromInteger
abs = fmap abs
signum = fmap signum
{-
{--------------------------------------------------------------------
......
......@@ -61,7 +61,7 @@ import Data.Functor
import Data.Functor.Identity ( Identity(..) )
import Data.Functor.Utils ( StateL(..), StateR(..) )
import Data.Monoid ( Dual(..), Sum(..), Product(..),
First(..), Last(..), Alt(..) )
First(..), Last(..), Alt(..), Ap(..) )
import Data.Proxy ( Proxy(..) )
import GHC.Arr
......@@ -294,6 +294,10 @@ instance Traversable Last where
instance (Traversable f) => Traversable (Alt f) where
traverse f (Alt x) = Alt <$> traverse f x
-- | @since 4.12.0.0
instance (Traversable f) => Traversable (Ap f) where
traverse f (Ap x) = Ap <$> traverse f x
-- | @since 4.9.0.0
instance Traversable ZipList where
traverse f (ZipList x) = ZipList <$> traverse f x
......
......@@ -19,9 +19,11 @@
* `Numeric.showEFloat (Just 0)` now respects the user's requested precision.
(#15115)
* `Data.Monoid.Alt` now has `Foldable` and `Traversable` instances. (#15099)
* `Data.Monoid.Ap` has been introduced
## 4.11.1.0 *TBA*
* Bundled with GHC 8.4.2
......
......@@ -10,7 +10,7 @@ annfail10.hs:9:1: error:
instance Data.Data.Data Ordering -- Defined in ‘Data.Data’
instance Data.Data.Data Integer -- Defined in ‘Data.Data’
...plus 15 others
...plus 45 instances involving out-of-scope types
...plus 46 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the annotation: {-# ANN f 1 #-}
......@@ -23,6 +23,6 @@ annfail10.hs:9:11: error:
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
...plus two others
...plus 17 instances involving out-of-scope types
...plus 18 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the annotation: {-# ANN f 1 #-}
......@@ -8,5 +8,5 @@
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
...plus two others
...plus five instances involving out-of-scope types
...plus six instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
......@@ -8,7 +8,7 @@ T13393.hs:61:3: error:
instance Traversable Identity -- Defined in ‘Data.Traversable’
instance Traversable Maybe -- Defined in ‘Data.Traversable’
...plus two others
...plus 26 instances involving out-of-scope types
...plus 27 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block:
mapM putBackLeftOverInputAndReturnOutput undefined
......
......@@ -12,7 +12,7 @@ T14273.hs:7:27: warning: [-Wdeferred-type-errors (in -Wdefault)]
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 67 instances involving out-of-scope types
...plus 68 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘Just’, namely ‘(show _a)’
In the expression: Just (show _a)
......@@ -62,7 +62,7 @@ T14273.hs:13:10: warning: [-Wdeferred-type-errors (in -Wdefault)]
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 67 instances involving out-of-scope types
...plus 68 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show (_h ++ [])
In an equation for ‘foo’: foo xs = show (_h ++ [])
......
......@@ -9,7 +9,7 @@ holes2.hs:3:5: warning: [-Wdeferred-type-errors (in -Wdefault)]
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 67 instances involving out-of-scope types
...plus 68 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show _
In an equation for ‘f’: f = show _
......
......@@ -104,7 +104,7 @@ valid_substitutions.hs:30:5: warning: [-Wdeferred-type-errors (in -Wdefault)]
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 67 instances involving out-of-scope types
...plus 68 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show _
In an equation for ‘f’: f = show _
......@@ -153,7 +153,7 @@ valid_substitutions.hs:34:5: warning: [-Wdeferred-type-errors (in -Wdefault)]
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 67 instances involving out-of-scope types
...plus 68 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show (_ (_ :: Bool))
In an equation for ‘h’: h = show (_ (_ :: Bool))
......
......@@ -11,7 +11,7 @@ T10971b.hs:4:11: error:
instance Foldable Maybe -- Defined in ‘Data.Foldable’
instance Foldable ((,) a) -- Defined in ‘Data.Foldable’
...plus one other
...plus 27 instances involving out-of-scope types
...plus 28 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: length x
In the expression: \ x -> length x
......@@ -29,7 +29,7 @@ T10971b.hs:5:13: error:
instance Traversable Maybe -- Defined in ‘Data.Traversable’
instance Traversable ((,) a) -- Defined in ‘Data.Traversable’
...plus one other
...plus 27 instances involving out-of-scope types
...plus 28 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: fmapDefault f x
In the expression: \ f x -> fmapDefault f x
......@@ -47,7 +47,7 @@ T10971b.hs:6:14: error:
instance Traversable Maybe -- Defined in ‘Data.Traversable’
instance Traversable ((,) a) -- Defined in ‘Data.Traversable’
...plus one other
...plus 27 instances involving out-of-scope types
...plus 28 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: fmapDefault f x
In the expression: (fmapDefault f x, length x)
......@@ -65,7 +65,7 @@ T10971b.hs:6:31: error:
instance Foldable Maybe -- Defined in ‘Data.Foldable’
instance Foldable ((,) a) -- Defined in ‘Data.Foldable’
...plus one other
...plus 27 instances involving out-of-scope types
...plus 28 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: length x
In the expression: (fmapDefault f x, length x)
......
......@@ -10,7 +10,7 @@ T12921.hs:4:1: error:
instance Data.Data.Data Ordering -- Defined in ‘Data.Data’
instance Data.Data.Data Integer -- Defined in ‘Data.Data’
...plus 15 others
...plus 45 instances involving out-of-scope types
...plus 46 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the annotation:
{-# ANN module "HLint: ignore Reduce duplication" #-}
......
......@@ -37,7 +37,7 @@ T14884.hs:4:7: error:
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 64 instances involving out-of-scope types
...plus 65 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘_’, namely ‘print’
In the expression: _ print "abc"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment