{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoImplicitPrelude #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Applicative
-- Copyright   :  Conor McBride and Ross Paterson 2005
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
--
-- This module describes a structure intermediate between a functor and
-- a monad (technically, a strong lax monoidal functor).  Compared with
-- monads, this interface lacks the full power of the binding operation
-- '>>=', but
--
-- * it has more instances.
--
-- * it is sufficient for many uses, e.g. context-free parsing, or the
--   'Data.Traversable.Traversable' class.
--
-- * instances can perform analysis of computations before they are
--   executed, and thus produce shared optimizations.
--
-- This interface was introduced for parsers by Niklas Röjemo, because
-- it admits more sharing than the monadic interface.  The names here are
-- mostly based on parsing work by Doaitse Swierstra.
--
-- For more details, see
-- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative Programming with Effects>,
-- by Conor McBride and Ross Paterson.

module Control.Applicative (
    -- * Applicative functors
    Applicative(..),
    -- * Alternatives
    Alternative(..),
    -- * Instances
    Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
    -- * Utility functions
    (<$>), (<$), (<**>),
    liftA, liftA3,
    optional,
    asum,
    ) where

import Control.Category hiding ((.), id)
import Control.Arrow
import Data.Maybe
import Data.Tuple
import Data.Eq
import Data.Ord
import Data.Foldable (Foldable(..), asum)
import Data.Functor ((<$>))
import Data.Functor.Const (Const(..))

import GHC.Base
import GHC.Generics
import GHC.List (repeat, zipWith, drop)
import GHC.Read (Read)
import GHC.Show (Show)

-- $setup
-- >>> import Prelude

newtype WrappedMonad m a = WrapMonad { forall (m :: * -> *) a. WrappedMonad m a -> m a
unwrapMonad :: m a }
                         deriving ( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (m :: * -> *) a x.
Rep (WrappedMonad m a) x -> WrappedMonad m a
forall (m :: * -> *) a x.
WrappedMonad m a -> Rep (WrappedMonad m a) x
$cto :: forall (m :: * -> *) a x.
Rep (WrappedMonad m a) x -> WrappedMonad m a
$cfrom :: forall (m :: * -> *) a x.
WrappedMonad m a -> Rep (WrappedMonad m a) x
Generic  -- ^ @since 4.7.0.0
                                  , forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (m :: * -> *) a. Rep1 (WrappedMonad m) a -> WrappedMonad m a
forall (m :: * -> *) a. WrappedMonad m a -> Rep1 (WrappedMonad m) a
$cto1 :: forall (m :: * -> *) a. Rep1 (WrappedMonad m) a -> WrappedMonad m a
$cfrom1 :: forall (m :: * -> *) a. WrappedMonad m a -> Rep1 (WrappedMonad m) a
Generic1 -- ^ @since 4.7.0.0
                                  , forall a. a -> WrappedMonad m a
forall a b.
WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b
forall a b.
WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b
forall {m :: * -> *}. Monad m => Applicative (WrappedMonad m)
forall (m :: * -> *) a. Monad m => a -> WrappedMonad m a
forall (m :: * -> *) a b.
Monad m =>
WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b
forall (m :: * -> *) a b.
Monad m =>
WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> WrappedMonad m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> WrappedMonad m a
>> :: forall a b.
WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b
>>= :: forall a b.
WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b
Monad    -- ^ @since 4.7.0.0
                                  )

-- | @since 2.01
instance Monad m => Functor (WrappedMonad m) where
    fmap :: forall a b. (a -> b) -> WrappedMonad m a -> WrappedMonad m b
fmap a -> b
f (WrapMonad m a
v) = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> b
f m a
v)

-- | @since 2.01
instance Monad m => Applicative (WrappedMonad m) where
    pure :: forall a. a -> WrappedMonad m a
pure = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
    WrapMonad m (a -> b)
f <*> :: forall a b.
WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b
<*> WrapMonad m a
v = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m (a -> b)
f forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
`ap` m a
v)
    liftA2 :: forall a b c.
(a -> b -> c)
-> WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m c
liftA2 a -> b -> c
f (WrapMonad m a
x) (WrapMonad m b
y) = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> b -> c
f m a
x m b
y)

-- | @since 2.01
instance MonadPlus m => Alternative (WrappedMonad m) where
    empty :: forall a. WrappedMonad m a
empty = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall (m :: * -> *) a. MonadPlus m => m a
mzero
    WrapMonad m a
u <|> :: forall a. WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a
<|> WrapMonad m a
v = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a
u forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` m a
v)

newtype WrappedArrow a b c = WrapArrow { forall (a :: * -> * -> *) b c. WrappedArrow a b c -> a b c
unwrapArrow :: a b c }
                           deriving ( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: * -> * -> *) b c x.
Rep (WrappedArrow a b c) x -> WrappedArrow a b c
forall (a :: * -> * -> *) b c x.
WrappedArrow a b c -> Rep (WrappedArrow a b c) x
$cto :: forall (a :: * -> * -> *) b c x.
Rep (WrappedArrow a b c) x -> WrappedArrow a b c
$cfrom :: forall (a :: * -> * -> *) b c x.
WrappedArrow a b c -> Rep (WrappedArrow a b c) x
Generic  -- ^ @since 4.7.0.0
                                    , forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (a :: * -> * -> *) b a.
Rep1 (WrappedArrow a b) a -> WrappedArrow a b a
forall (a :: * -> * -> *) b a.
WrappedArrow a b a -> Rep1 (WrappedArrow a b) a
$cto1 :: forall (a :: * -> * -> *) b a.
Rep1 (WrappedArrow a b) a -> WrappedArrow a b a
$cfrom1 :: forall (a :: * -> * -> *) b a.
WrappedArrow a b a -> Rep1 (WrappedArrow a b) a
Generic1 -- ^ @since 4.7.0.0
                                    )

-- | @since 2.01
instance Arrow a => Functor (WrappedArrow a b) where
    fmap :: forall a b. (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b
fmap a -> b
f (WrapArrow a b a
a) = forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
a forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr a -> b
f)

-- | @since 2.01
instance Arrow a => Applicative (WrappedArrow a b) where
    pure :: forall a. a -> WrappedArrow a b a
pure a
x = forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall a b. a -> b -> a
const a
x))
    liftA2 :: forall a b c.
(a -> b -> c)
-> WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b c
liftA2 a -> b -> c
f (WrapArrow a b a
u) (WrapArrow a b b
v) =
      forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a b b
v forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c
f))

-- | @since 2.01
instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
    empty :: forall a. WrappedArrow a b a
empty = forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
    WrapArrow a b a
u <|> :: forall a.
WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a
<|> WrapArrow a b a
v = forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u forall (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+> a b a
v)

-- | Lists, but with an 'Applicative' functor based on zipping.
newtype ZipList a = ZipList { forall a. ZipList a -> [a]
getZipList :: [a] }
                  deriving ( Int -> ZipList a -> ShowS
forall a. Show a => Int -> ZipList a -> ShowS
forall a. Show a => [ZipList a] -> ShowS
forall a. Show a => ZipList a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ZipList a] -> ShowS
$cshowList :: forall a. Show a => [ZipList a] -> ShowS
show :: ZipList a -> String
$cshow :: forall a. Show a => ZipList a -> String
showsPrec :: Int -> ZipList a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ZipList a -> ShowS
Show     -- ^ @since 4.7.0.0
                           , ZipList a -> ZipList a -> Bool
forall a. Eq a => ZipList a -> ZipList a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ZipList a -> ZipList a -> Bool
$c/= :: forall a. Eq a => ZipList a -> ZipList a -> Bool
== :: ZipList a -> ZipList a -> Bool
$c== :: forall a. Eq a => ZipList a -> ZipList a -> Bool
Eq       -- ^ @since 4.7.0.0
                           , ZipList a -> ZipList a -> Bool
ZipList a -> ZipList a -> Ordering
ZipList a -> ZipList a -> ZipList a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (ZipList a)
forall a. Ord a => ZipList a -> ZipList a -> Bool
forall a. Ord a => ZipList a -> ZipList a -> Ordering
forall a. Ord a => ZipList a -> ZipList a -> ZipList a
min :: ZipList a -> ZipList a -> ZipList a
$cmin :: forall a. Ord a => ZipList a -> ZipList a -> ZipList a
max :: ZipList a -> ZipList a -> ZipList a
$cmax :: forall a. Ord a => ZipList a -> ZipList a -> ZipList a
>= :: ZipList a -> ZipList a -> Bool
$c>= :: forall a. Ord a => ZipList a -> ZipList a -> Bool
> :: ZipList a -> ZipList a -> Bool
$c> :: forall a. Ord a => ZipList a -> ZipList a -> Bool
<= :: ZipList a -> ZipList a -> Bool
$c<= :: forall a. Ord a => ZipList a -> ZipList a -> Bool
< :: ZipList a -> ZipList a -> Bool
$c< :: forall a. Ord a => ZipList a -> ZipList a -> Bool
compare :: ZipList a -> ZipList a -> Ordering
$ccompare :: forall a. Ord a => ZipList a -> ZipList a -> Ordering
Ord      -- ^ @since 4.7.0.0
                           , ReadPrec [ZipList a]
ReadPrec (ZipList a)
ReadS [ZipList a]
forall a. Read a => ReadPrec [ZipList a]
forall a. Read a => ReadPrec (ZipList a)
forall a. Read a => Int -> ReadS (ZipList a)
forall a. Read a => ReadS [ZipList a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ZipList a]
$creadListPrec :: forall a. Read a => ReadPrec [ZipList a]
readPrec :: ReadPrec (ZipList a)
$creadPrec :: forall a. Read a => ReadPrec (ZipList a)
readList :: ReadS [ZipList a]
$creadList :: forall a. Read a => ReadS [ZipList a]
readsPrec :: Int -> ReadS (ZipList a)
$creadsPrec :: forall a. Read a => Int -> ReadS (ZipList a)
Read     -- ^ @since 4.7.0.0
                           , forall a b. a -> ZipList b -> ZipList a
forall a b. (a -> b) -> ZipList a -> ZipList b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ZipList b -> ZipList a
$c<$ :: forall a b. a -> ZipList b -> ZipList a
fmap :: forall a b. (a -> b) -> ZipList a -> ZipList b
$cfmap :: forall a b. (a -> b) -> ZipList a -> ZipList b
Functor  -- ^ @since 2.01
                           , forall a. Eq a => a -> ZipList a -> Bool
forall a. Num a => ZipList a -> a
forall a. Ord a => ZipList a -> a
forall m. Monoid m => ZipList m -> m
forall a. ZipList a -> Bool
forall a. ZipList a -> Int
forall a. ZipList a -> [a]
forall a. (a -> a -> a) -> ZipList a -> a
forall m a. Monoid m => (a -> m) -> ZipList a -> m
forall b a. (b -> a -> b) -> b -> ZipList a -> b
forall a b. (a -> b -> b) -> b -> ZipList a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => ZipList a -> a
$cproduct :: forall a. Num a => ZipList a -> a
sum :: forall a. Num a => ZipList a -> a
$csum :: forall a. Num a => ZipList a -> a
minimum :: forall a. Ord a => ZipList a -> a
$cminimum :: forall a. Ord a => ZipList a -> a
maximum :: forall a. Ord a => ZipList a -> a
$cmaximum :: forall a. Ord a => ZipList a -> a
elem :: forall a. Eq a => a -> ZipList a -> Bool
$celem :: forall a. Eq a => a -> ZipList a -> Bool
length :: forall a. ZipList a -> Int
$clength :: forall a. ZipList a -> Int
null :: forall a. ZipList a -> Bool
$cnull :: forall a. ZipList a -> Bool
toList :: forall a. ZipList a -> [a]
$ctoList :: forall a. ZipList a -> [a]
foldl1 :: forall a. (a -> a -> a) -> ZipList a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ZipList a -> a
foldr1 :: forall a. (a -> a -> a) -> ZipList a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> ZipList a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> ZipList a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> ZipList a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ZipList a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> ZipList a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ZipList a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> ZipList a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ZipList a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> ZipList a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
fold :: forall m. Monoid m => ZipList m -> m
$cfold :: forall m. Monoid m => ZipList m -> m
Foldable -- ^ @since 4.9.0.0
                           , forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ZipList a) x -> ZipList a
forall a x. ZipList a -> Rep (ZipList a) x
$cto :: forall a x. Rep (ZipList a) x -> ZipList a
$cfrom :: forall a x. ZipList a -> Rep (ZipList a) x
Generic  -- ^ @since 4.7.0.0
                           , forall a. Rep1 ZipList a -> ZipList a
forall a. ZipList a -> Rep1 ZipList a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 ZipList a -> ZipList a
$cfrom1 :: forall a. ZipList a -> Rep1 ZipList a
Generic1 -- ^ @since 4.7.0.0
                           )
-- See Data.Traversable for Traversable instance due to import loops

-- |
-- > f <$> ZipList xs1 <*> ... <*> ZipList xsN
-- >     = ZipList (zipWithN f xs1 ... xsN)
--
-- where @zipWithN@ refers to the @zipWith@ function of the appropriate arity
-- (@zipWith@, @zipWith3@, @zipWith4@, ...). For example:
--
-- > (\a b c -> stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..]
-- >     = ZipList (zipWith3 (\a b c -> stimes c [a, b]) "abcd" "567" [1..])
-- >     = ZipList {getZipList = ["a5","b6b6","c7c7c7"]}
--
-- @since 2.01
instance Applicative ZipList where
    pure :: forall a. a -> ZipList a
pure a
x = forall a. [a] -> ZipList a
ZipList (forall a. a -> [a]
repeat a
x)
    liftA2 :: forall a b c. (a -> b -> c) -> ZipList a -> ZipList b -> ZipList c
liftA2 a -> b -> c
f (ZipList [a]
xs) (ZipList [b]
ys) = forall a. [a] -> ZipList a
ZipList (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> c
f [a]
xs [b]
ys)

-- | @since 4.11.0.0
instance Alternative ZipList where
   empty :: forall a. ZipList a
empty = forall a. [a] -> ZipList a
ZipList []
   ZipList [a]
xs <|> :: forall a. ZipList a -> ZipList a -> ZipList a
<|> ZipList [a]
ys = forall a. [a] -> ZipList a
ZipList ([a]
xs forall a. [a] -> [a] -> [a]
++ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) [a]
ys)

-- extra functions

-- | One or none.
--
-- It is useful for modelling any computation that is allowed to fail.
--
-- ==== __Examples__
--
-- Using the 'Alternative' instance of "Control.Monad.Except", the following functions:
--
-- >>> import Control.Monad.Except
--
-- >>> canFail = throwError "it failed" :: Except String Int
-- >>> final = return 42                :: Except String Int
--
-- Can be combined by allowing the first function to fail:
--
-- >>> runExcept $ canFail *> final
-- Left "it failed"
-- >>> runExcept $ optional canFail *> final
-- Right 42

optional :: Alternative f => f a -> f (Maybe a)
optional :: forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional f a
v = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
v forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing