{-# 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   :  stable
-- 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)
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 x. WrappedMonad m a -> Rep (WrappedMonad m a) x)
-> (forall x. Rep (WrappedMonad m a) x -> WrappedMonad m a)
-> Generic (WrappedMonad m a)
forall x. Rep (WrappedMonad m a) x -> WrappedMonad m a
forall x. WrappedMonad m a -> Rep (WrappedMonad m a) x
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
$cfrom :: 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
Generic  -- ^ @since 4.7.0.0
                                  , (forall a. WrappedMonad m a -> Rep1 (WrappedMonad m) a)
-> (forall a. Rep1 (WrappedMonad m) a -> WrappedMonad m a)
-> Generic1 (WrappedMonad m)
forall a. Rep1 (WrappedMonad m) a -> WrappedMonad m a
forall a. WrappedMonad m a -> Rep1 (WrappedMonad m) a
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
$cfrom1 :: forall (m :: * -> *) a. WrappedMonad m a -> Rep1 (WrappedMonad m) a
$cto1 :: forall (m :: * -> *) a. Rep1 (WrappedMonad m) a -> WrappedMonad m a
Generic1 -- ^ @since 4.7.0.0
                                  , Applicative (WrappedMonad m)
Applicative (WrappedMonad m)
-> (forall a b.
    WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b)
-> (forall a b.
    WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b)
-> (forall a. a -> WrappedMonad m a)
-> Monad (WrappedMonad m)
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
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
WrappedMonad m a -> (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 -> WrappedMonad m b -> WrappedMonad m b
>> :: forall a b.
WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> WrappedMonad m a
return :: forall a. a -> WrappedMonad m a
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) = 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 :: forall a. 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 a. a -> m a
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 = 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 :: 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) = 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 :: forall a. WrappedMonad m a
empty = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad m a
forall a. m a
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 = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a
u m a -> m a -> m a
forall a. 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 { forall (a :: * -> * -> *) b c. WrappedArrow a b c -> a b c
unwrapArrow :: a b c }
                           deriving ( (forall x. WrappedArrow a b c -> Rep (WrappedArrow a b c) x)
-> (forall x. Rep (WrappedArrow a b c) x -> WrappedArrow a b c)
-> Generic (WrappedArrow a b c)
forall x. Rep (WrappedArrow a b c) x -> WrappedArrow a b c
forall x. WrappedArrow a b c -> Rep (WrappedArrow a b c) x
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
$cfrom :: 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
Generic  -- ^ @since 4.7.0.0
                                    , (forall a. WrappedArrow a b a -> Rep1 (WrappedArrow a b) a)
-> (forall a. Rep1 (WrappedArrow a b) a -> WrappedArrow a b a)
-> Generic1 (WrappedArrow a b)
forall a. Rep1 (WrappedArrow a b) a -> WrappedArrow a b a
forall a. WrappedArrow a b a -> Rep1 (WrappedArrow a b) a
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
$cfrom1 :: 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
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) = 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 b c. (b -> c) -> a b 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 = 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 b c. (b -> c) -> a b c
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 :: 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) =
      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 b c c'. a b c -> a b c' -> a b (c, c')
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 b c. (b -> c) -> 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 :: forall a. 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 b c. a b c
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 = 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 b c. a b c -> a b c -> a b c
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
[ZipList a] -> ShowS
ZipList a -> String
(Int -> ZipList a -> ShowS)
-> (ZipList a -> String)
-> ([ZipList a] -> ShowS)
-> Show (ZipList a)
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
$cshowsPrec :: forall a. Show a => Int -> ZipList a -> ShowS
showsPrec :: Int -> ZipList a -> ShowS
$cshow :: forall a. Show a => ZipList a -> String
show :: ZipList a -> String
$cshowList :: forall a. Show a => [ZipList a] -> ShowS
showList :: [ZipList a] -> ShowS
Show     -- ^ @since 4.7.0.0
                           , ZipList a -> ZipList a -> Bool
(ZipList a -> ZipList a -> Bool)
-> (ZipList a -> ZipList a -> Bool) -> Eq (ZipList a)
forall a. Eq a => ZipList a -> ZipList a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
/= :: ZipList a -> ZipList a -> Bool
Eq       -- ^ @since 4.7.0.0
                           , Eq (ZipList a)
Eq (ZipList a)
-> (ZipList a -> ZipList a -> Ordering)
-> (ZipList a -> ZipList a -> Bool)
-> (ZipList a -> ZipList a -> Bool)
-> (ZipList a -> ZipList a -> Bool)
-> (ZipList a -> ZipList a -> Bool)
-> (ZipList a -> ZipList a -> ZipList a)
-> (ZipList a -> ZipList a -> ZipList a)
-> Ord (ZipList a)
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
$ccompare :: forall a. Ord a => ZipList a -> ZipList a -> Ordering
compare :: ZipList a -> ZipList a -> Ordering
$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
>= :: ZipList a -> ZipList a -> Bool
$cmax :: forall a. Ord a => ZipList a -> ZipList a -> ZipList a
max :: ZipList a -> ZipList a -> ZipList a
$cmin :: forall a. Ord a => ZipList a -> ZipList a -> ZipList a
min :: ZipList a -> ZipList a -> ZipList a
Ord      -- ^ @since 4.7.0.0
                           , ReadPrec [ZipList a]
ReadPrec (ZipList a)
Int -> ReadS (ZipList a)
ReadS [ZipList a]
(Int -> ReadS (ZipList a))
-> ReadS [ZipList a]
-> ReadPrec (ZipList a)
-> ReadPrec [ZipList a]
-> Read (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
$creadsPrec :: forall a. Read a => Int -> ReadS (ZipList a)
readsPrec :: Int -> ReadS (ZipList a)
$creadList :: forall a. Read a => ReadS [ZipList a]
readList :: ReadS [ZipList a]
$creadPrec :: forall a. Read a => ReadPrec (ZipList a)
readPrec :: ReadPrec (ZipList a)
$creadListPrec :: forall a. Read a => ReadPrec [ZipList a]
readListPrec :: ReadPrec [ZipList a]
Read     -- ^ @since 4.7.0.0
                           , (forall a b. (a -> b) -> ZipList a -> ZipList b)
-> (forall a b. a -> ZipList b -> ZipList a) -> Functor ZipList
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
$cfmap :: forall a b. (a -> b) -> ZipList a -> ZipList b
fmap :: forall a b. (a -> b) -> ZipList a -> ZipList b
$c<$ :: forall a b. a -> ZipList b -> ZipList a
<$ :: forall a b. a -> ZipList b -> ZipList a
Functor  -- ^ @since 2.01
                           , (forall m. Monoid m => ZipList m -> m)
-> (forall m a. Monoid m => (a -> m) -> ZipList a -> m)
-> (forall m a. Monoid m => (a -> m) -> ZipList a -> m)
-> (forall a b. (a -> b -> b) -> b -> ZipList a -> b)
-> (forall a b. (a -> b -> b) -> b -> ZipList a -> b)
-> (forall b a. (b -> a -> b) -> b -> ZipList a -> b)
-> (forall b a. (b -> a -> b) -> b -> ZipList a -> b)
-> (forall a. (a -> a -> a) -> ZipList a -> a)
-> (forall a. (a -> a -> a) -> ZipList a -> a)
-> (forall a. ZipList a -> [a])
-> (forall a. ZipList a -> Bool)
-> (forall a. ZipList a -> Int)
-> (forall a. Eq a => a -> ZipList a -> Bool)
-> (forall a. Ord a => ZipList a -> a)
-> (forall a. Ord a => ZipList a -> a)
-> (forall a. Num a => ZipList a -> a)
-> (forall a. Num a => ZipList a -> a)
-> Foldable ZipList
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
$cfold :: forall m. Monoid m => ZipList m -> m
fold :: forall m. Monoid m => ZipList m -> 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
foldMap' :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
$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
foldr' :: forall a b. (a -> b -> 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
foldl' :: forall b a. (b -> a -> b) -> b -> ZipList a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> ZipList a -> a
foldr1 :: forall a. (a -> a -> a) -> ZipList a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ZipList a -> a
foldl1 :: forall a. (a -> a -> a) -> ZipList a -> a
$ctoList :: forall a. ZipList a -> [a]
toList :: forall a. ZipList a -> [a]
$cnull :: forall a. ZipList a -> Bool
null :: forall a. ZipList a -> Bool
$clength :: forall a. ZipList a -> Int
length :: forall a. ZipList a -> Int
$celem :: forall a. Eq a => a -> ZipList a -> Bool
elem :: forall a. Eq a => a -> ZipList a -> Bool
$cmaximum :: forall a. Ord a => ZipList a -> a
maximum :: forall a. Ord a => ZipList a -> a
$cminimum :: forall a. Ord a => ZipList a -> a
minimum :: forall a. Ord a => ZipList a -> a
$csum :: forall a. Num a => ZipList a -> a
sum :: forall a. Num a => ZipList a -> a
$cproduct :: forall a. Num a => ZipList a -> a
product :: forall a. Num a => ZipList a -> a
Foldable -- ^ @since 4.9.0.0
                           , (forall x. ZipList a -> Rep (ZipList a) x)
-> (forall x. Rep (ZipList a) x -> ZipList a)
-> Generic (ZipList a)
forall x. Rep (ZipList a) x -> ZipList a
forall x. ZipList a -> Rep (ZipList a) x
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
$cfrom :: forall a x. ZipList a -> Rep (ZipList a) x
$cto :: forall a x. Rep (ZipList a) x -> ZipList a
Generic  -- ^ @since 4.7.0.0
                           , (forall a. ZipList a -> Rep1 ZipList a)
-> (forall a. Rep1 ZipList a -> ZipList a) -> Generic1 ZipList
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
$cfrom1 :: forall a. ZipList a -> Rep1 ZipList a
$cto1 :: forall a. Rep1 ZipList a -> 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 = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList (a -> [a]
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) = [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 :: forall a. ZipList a
empty = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList []
   ZipList [a]
xs0 <|> :: forall a. ZipList a -> ZipList a -> ZipList a
<|> ZipList [a]
ys0 = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList ([a] -> ZipList a) -> [a] -> ZipList a
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> [a]
forall {a}. [a] -> [a] -> [a]
go [a]
xs0 [a]
ys0
     where
       go :: [a] -> [a] -> [a]
go (a
x:[a]
xs) (a
_:[a]
ys) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
go [a]
xs [a]
ys
       go    []     [a]
ys  = [a]
ys
       go    [a]
xs      [a]
_  = [a]
xs

-- 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 = 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 a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing