semigroupoids-6.0.0.1: Semigroupoids: Category sans id

Copyright | (C) 2011-2015 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Functor.Alt

Description

Synopsis

- class Functor f => Alt (f :: Type -> Type) where
  - (<!>) :: f a -> f a -> f a
  - some :: Applicative f => f a -> f [a]
  - many :: Applicative f => f a -> f [a]
- optional :: (Alt f, Applicative f) => f a -> f (Maybe a)
- galt :: (Generic1 f, Alt (Rep1 f)) => f a -> f a -> f a
- module Data.Functor.Apply

# Documentation

class Functor f => Alt (f :: Type -> Type) where #

Laws:

    <!> is associative:             (a <!> b) <!> c = a <!> (b <!> c)
    <$> left-distributes over <!>:  f <$> (a <!> b) = (f <$> a) <!> (f <$> b)

If extended to an `Alternative` then `<!>` should equal `<|>`.

Ideally, an instance of `Alt` also satisfies the "left distribution" law of MonadPlus with respect to `<.>`:

    <.> right-distributes over <!>: (a <!> b) <.> c = (a <.> c) <!> (b <.> c)

`IO`, ``Either` a`, ``ExceptT` e m` and `STM` instead satisfy the "left catch" law:

    pure a <!> b = pure a

`Maybe` and `Identity` satisfy both "left distribution" and "left catch".

These variations cannot be stated purely in terms of the dependencies of `Alt`.

When and if MonadPlus is successfully refactored, this class should also be refactored to remove these instances.

The right distributive law should extend in the cases where the a `Bind` or `Monad` is provided to yield variations of the right distributive law:

    (m <!> n) >>- f = (m >>- f) <!> (m >>- f)
    (m <!> n) >>= f = (m >>= f) <!> (m >>= f)

Minimal complete definition

(<!>)

Methods

(<!>) :: f a -> f a -> f a infixl 3 #

`<|>` without a required `empty`

some :: Applicative f => f a -> f [a] #

many :: Applicative f => f a -> f [a] #

#### Instances

Instances details

Alt Identity # | Choose the first option every time. While 'choose the last option' every time is also valid, this instance satisfies more laws. Since: 5.3.6
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Identity a -> Identity a -> Identity a # some :: Applicative Identity => Identity a -> Identity [a] # many :: Applicative Identity => Identity a -> Identity [a] #
Alt First # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: First a -> First a -> First a # some :: Applicative First => First a -> First [a] # many :: Applicative First => First a -> First [a] #
Alt Last # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Last a -> Last a -> Last a # some :: Applicative Last => Last a -> Last [a] # many :: Applicative Last => Last a -> Last [a] #
Alt First # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: First a -> First a -> First a # some :: Applicative First => First a -> First [a] # many :: Applicative First => First a -> First [a] #
Alt Last # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Last a -> Last a -> Last a # some :: Applicative Last => Last a -> Last [a] # many :: Applicative Last => Last a -> Last [a] #
Alt NonEmpty # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: NonEmpty a -> NonEmpty a -> NonEmpty a # some :: Applicative NonEmpty => NonEmpty a -> NonEmpty [a] # many :: Applicative NonEmpty => NonEmpty a -> NonEmpty [a] #
Alt IntMap # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: IntMap a -> IntMap a -> IntMap a # some :: Applicative IntMap => IntMap a -> IntMap [a] # many :: Applicative IntMap => IntMap a -> IntMap [a] #
Alt Seq # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Seq a -> Seq a -> Seq a # some :: Applicative Seq => Seq a -> Seq [a] # many :: Applicative Seq => Seq a -> Seq [a] #
Alt IO # | This instance does not actually satisfy the (`<.>`) right distributive law It instead satisfies the "left catch" law
Instance details Defined in Data.Functor.Alt Methods (<!>) :: IO a -> IO a -> IO a # some :: Applicative IO => IO a -> IO [a] # many :: Applicative IO => IO a -> IO [a] #
Alt Maybe # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Maybe a -> Maybe a -> Maybe a # some :: Applicative Maybe => Maybe a -> Maybe [a] # many :: Applicative Maybe => Maybe a -> Maybe [a] #
Alt [] # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: [a] -> [a] -> [a] # some :: Applicative [] => [a] -> [[a]] # many :: Applicative [] => [a] -> [[a]] #
MonadPlus m => Alt (WrappedMonad m) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a # some :: Applicative (WrappedMonad m) => WrappedMonad m a -> WrappedMonad m [a] # many :: Applicative (WrappedMonad m) => WrappedMonad m a -> WrappedMonad m [a] #
Alt (Either a) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Either a a0 -> Either a a0 -> Either a a0 # some :: Applicative (Either a) => Either a a0 -> Either a [a0] # many :: Applicative (Either a) => Either a a0 -> Either a [a0] #
Alt (Proxy :: Type -> Type) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Proxy a -> Proxy a -> Proxy a # some :: Applicative (Proxy :: Type -> Type) => Proxy a -> Proxy [a] # many :: Applicative (Proxy :: Type -> Type) => Proxy a -> Proxy [a] #
Alt (U1 :: Type -> Type) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: U1 a -> U1 a -> U1 a # some :: Applicative (U1 :: Type -> Type) => U1 a -> U1 [a] # many :: Applicative (U1 :: Type -> Type) => U1 a -> U1 [a] #
Alt (V1 :: Type -> Type) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: V1 a -> V1 a -> V1 a # some :: Applicative (V1 :: Type -> Type) => V1 a -> V1 [a] # many :: Applicative (V1 :: Type -> Type) => V1 a -> V1 [a] #
Ord k => Alt (Map k) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Map k a -> Map k a -> Map k a # some :: Applicative (Map k) => Map k a -> Map k [a] # many :: Applicative (Map k) => Map k a -> Map k [a] #
Alternative f => Alt (WrappedApplicative f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WrappedApplicative f a -> WrappedApplicative f a -> WrappedApplicative f a # some :: Applicative (WrappedApplicative f) => WrappedApplicative f a -> WrappedApplicative f [a] # many :: Applicative (WrappedApplicative f) => WrappedApplicative f a -> WrappedApplicative f [a] #
Alt f => Alt (Lift f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Lift f a -> Lift f a -> Lift f a # some :: Applicative (Lift f) => Lift f a -> Lift f [a] # many :: Applicative (Lift f) => Lift f a -> Lift f [a] #
(Functor f, Monad f) => Alt (MaybeT f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: MaybeT f a -> MaybeT f a -> MaybeT f a # some :: Applicative (MaybeT f) => MaybeT f a -> MaybeT f [a] # many :: Applicative (MaybeT f) => MaybeT f a -> MaybeT f [a] #
(Hashable k, Eq k) => Alt (HashMap k) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: HashMap k a -> HashMap k a -> HashMap k a # some :: Applicative (HashMap k) => HashMap k a -> HashMap k [a] # many :: Applicative (HashMap k) => HashMap k a -> HashMap k [a] #
ArrowPlus a => Alt (WrappedArrow a b) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WrappedArrow a b a0 -> WrappedArrow a b a0 -> WrappedArrow a b a0 # some :: Applicative (WrappedArrow a b) => WrappedArrow a b a0 -> WrappedArrow a b [a0] # many :: Applicative (WrappedArrow a b) => WrappedArrow a b a0 -> WrappedArrow a b [a0] #
Alt f => Alt (Rec1 f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Rec1 f a -> Rec1 f a -> Rec1 f a # some :: Applicative (Rec1 f) => Rec1 f a -> Rec1 f [a] # many :: Applicative (Rec1 f) => Rec1 f a -> Rec1 f [a] #
Alt f => Alt (Static f a) # |
Instance details Defined in Data.Semigroupoid.Static Methods (<!>) :: Static f a a0 -> Static f a a0 -> Static f a a0 # some :: Applicative (Static f a) => Static f a a0 -> Static f a [a0] # many :: Applicative (Static f a) => Static f a a0 -> Static f a [a0] #
Alt f => Alt (Backwards f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Backwards f a -> Backwards f a -> Backwards f a # some :: Applicative (Backwards f) => Backwards f a -> Backwards f [a] # many :: Applicative (Backwards f) => Backwards f a -> Backwards f [a] #
(Functor f, Monad f, Semigroup e) => Alt (ExceptT e f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: ExceptT e f a -> ExceptT e f a -> ExceptT e f a # some :: Applicative (ExceptT e f) => ExceptT e f a -> ExceptT e f [a] # many :: Applicative (ExceptT e f) => ExceptT e f a -> ExceptT e f [a] #
Alt f => Alt (IdentityT f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: IdentityT f a -> IdentityT f a -> IdentityT f a # some :: Applicative (IdentityT f) => IdentityT f a -> IdentityT f [a] # many :: Applicative (IdentityT f) => IdentityT f a -> IdentityT f [a] #
Alt f => Alt (ReaderT e f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: ReaderT e f a -> ReaderT e f a -> ReaderT e f a # some :: Applicative (ReaderT e f) => ReaderT e f a -> ReaderT e f [a] # many :: Applicative (ReaderT e f) => ReaderT e f a -> ReaderT e f [a] #
Alt f => Alt (StateT e f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: StateT e f a -> StateT e f a -> StateT e f a # some :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] # many :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #
Alt f => Alt (StateT e f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: StateT e f a -> StateT e f a -> StateT e f a # some :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] # many :: Applicative (StateT e f) => StateT e f a -> StateT e f [a] #
Alt f => Alt (WriterT w f) # | Since: 5.3.6
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WriterT w f a -> WriterT w f a -> WriterT w f a # some :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] # many :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #
Alt f => Alt (WriterT w f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WriterT w f a -> WriterT w f a -> WriterT w f a # some :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] # many :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #
Alt f => Alt (WriterT w f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: WriterT w f a -> WriterT w f a -> WriterT w f a # some :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] # many :: Applicative (WriterT w f) => WriterT w f a -> WriterT w f [a] #
Alt f => Alt (Reverse f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Reverse f a -> Reverse f a -> Reverse f a # some :: Applicative (Reverse f) => Reverse f a -> Reverse f [a] # many :: Applicative (Reverse f) => Reverse f a -> Reverse f [a] #
(Alt f, Alt g) => Alt (Product f g) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Product f g a -> Product f g a -> Product f g a # some :: Applicative (Product f g) => Product f g a -> Product f g [a] # many :: Applicative (Product f g) => Product f g a -> Product f g [a] #
(Alt f, Alt g) => Alt (f :*: g) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: (f :*: g) a -> (f :*: g) a -> (f :*: g) a # some :: Applicative (f :*: g) => (f :*: g) a -> (f :*: g) [a] # many :: Applicative (f :*: g) => (f :*: g) a -> (f :*: g) [a] #

Semigroup c => Alt (K1 i c :: Type -> Type) #

    since 5.3.8

Instance details Defined in Data.Functor.Alt Methods (<!>) :: K1 i c a -> K1 i c a -> K1 i c a # some :: Applicative (K1 i c :: Type -> Type) => K1 i c a -> K1 i c [a] # many :: Applicative (K1 i c :: Type -> Type) => K1 i c a -> K1 i c [a] #
(Alt f, Functor g) => Alt (Compose f g) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: Compose f g a -> Compose f g a -> Compose f g a # some :: Applicative (Compose f g) => Compose f g a -> Compose f g [a] # many :: Applicative (Compose f g) => Compose f g a -> Compose f g [a] #
(Alt f, Functor g) => Alt (f :.: g) # | Since: 5.3.8
Instance details Defined in Data.Functor.Alt Methods (<!>) :: (f :.: g) a -> (f :.: g) a -> (f :.: g) a # some :: Applicative (f :.: g) => (f :.: g) a -> (f :.: g) [a] # many :: Applicative (f :.: g) => (f :.: g) a -> (f :.: g) [a] #
Alt f => Alt (M1 i c f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: M1 i c f a -> M1 i c f a -> M1 i c f a # some :: Applicative (M1 i c f) => M1 i c f a -> M1 i c f [a] # many :: Applicative (M1 i c f) => M1 i c f a -> M1 i c f [a] #
Alt f => Alt (RWST r w s f) # | Since: 5.3.6
Instance details Defined in Data.Functor.Alt Methods (<!>) :: RWST r w s f a -> RWST r w s f a -> RWST r w s f a # some :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] # many :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #
Alt f => Alt (RWST r w s f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: RWST r w s f a -> RWST r w s f a -> RWST r w s f a # some :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] # many :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #
Alt f => Alt (RWST r w s f) # |
Instance details Defined in Data.Functor.Alt Methods (<!>) :: RWST r w s f a -> RWST r w s f a -> RWST r w s f a # some :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] # many :: Applicative (RWST r w s f) => RWST r w s f a -> RWST r w s f [a] #

optional :: (Alt f, Applicative f) => f a -> f (Maybe a) #

One or none.

galt :: (Generic1 f, Alt (Rep1 f)) => f a -> f a -> f a #

Generic (`<!>`). Caveats:

1. Will not compile if `f` is a sum type.
2. Any types where the `a` does not appear must have a `Semigroup` instance.

Since: 5.3.8

module Data.Functor.Apply