{-# 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,
    ) 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(..))
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)

newtype WrappedMonad m a = WrapMonad { WrappedMonad m a -> m a
unwrapMonad :: m a }
                         deriving ( Generic  -- ^ @since 4.7.0.0
                                  , Generic1 -- ^ @since 4.7.0.0
                                  , Monad    -- ^ @since 4.7.0.0
                                  )

-- | @since 2.01
instance Monad m => Functor (WrappedMonad m) where
    fmap :: (a -> b) -> WrappedMonad m a -> WrappedMonad m b
fmap a -> b
f (WrapMonad m a
v) = m b -> WrappedMonad m b
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad ((a -> b) -> m a -> m b
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 :: a -> WrappedMonad m a
pure = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a -> WrappedMonad m a) -> (a -> m a) -> a -> WrappedMonad m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    WrapMonad m (a -> b)
f <*> :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b
<*> WrapMonad m a
v = m b -> WrappedMonad m b
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m (a -> b)
f m (a -> b) -> m a -> m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
`ap` m a
v)
    liftA2 :: (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) = m c -> WrappedMonad m c
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad ((a -> b -> c) -> m a -> m b -> m c
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 :: WrappedMonad m a
empty = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    WrapMonad m a
u <|> :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a
<|> WrapMonad m a
v = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a
u m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` m a
v)

newtype WrappedArrow a b c = WrapArrow { WrappedArrow a b c -> a b c
unwrapArrow :: a b c }
                           deriving ( Generic  -- ^ @since 4.7.0.0
                                    , Generic1 -- ^ @since 4.7.0.0
                                    )

-- | @since 2.01
instance Arrow a => Functor (WrappedArrow a b) where
    fmap :: (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b
fmap a -> b
f (WrapArrow a b a
a) = a b b -> WrappedArrow a b b
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
a a b a -> a a b -> a b b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a -> b) -> a a b
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 :: a -> WrappedArrow a b a
pure a
x = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow ((b -> a) -> a b a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> b -> a
forall a b. a -> b -> a
const a
x))
    liftA2 :: (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) =
      a b c -> WrappedArrow a b c
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u a b a -> a b b -> a b (a, b)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a b b
v a b (a, b) -> a (a, b) c -> a b c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((a, b) -> c) -> a (a, b) c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((a -> b -> c) -> (a, b) -> c
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 :: WrappedArrow a b a
empty = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow a b a
forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
    WrapArrow a b a
u <|> :: WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a
<|> WrapArrow a b a
v = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u a b a -> a b a -> a b a
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 { ZipList a -> [a]
getZipList :: [a] }
                  deriving ( Show     -- ^ @since 4.7.0.0
                           , Eq       -- ^ @since 4.7.0.0
                           , Ord      -- ^ @since 4.7.0.0
                           , Read     -- ^ @since 4.7.0.0
                           , Functor  -- ^ @since 2.01
                           , Foldable -- ^ @since 4.9.0.0
                           , Generic  -- ^ @since 4.7.0.0
                           , 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 :: a -> ZipList a
pure a
x = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList (a -> [a]
forall a. a -> [a]
repeat a
x)
    liftA2 :: (a -> b -> c) -> ZipList a -> ZipList b -> ZipList c
liftA2 a -> b -> c
f (ZipList [a]
xs) (ZipList [b]
ys) = [c] -> ZipList c
forall a. [a] -> ZipList a
ZipList ((a -> b -> c) -> [a] -> [b] -> [c]
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 :: ZipList a
empty = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList []
   ZipList [a]
xs <|> :: ZipList a -> ZipList a -> ZipList a
<|> ZipList [a]
ys = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList ([a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) [a]
ys)

-- extra functions

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