| 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 | Safe |
| Language | Haskell98 |
Data.Functor.Bind
Description
- class Functor f where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- ($>) :: Functor f => f a -> b -> f b
- class Functor f => Apply f where
- (<..>) :: Apply w => w a -> w (a -> b) -> w b
- liftF2 :: Apply w => (a -> b -> c) -> w a -> w b -> w c
- liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d
- newtype WrappedApplicative f a = WrapApplicative {
- unwrapApplicative :: f a
- newtype MaybeApply f a = MaybeApply {
- runMaybeApply :: Either (f a) a
- class Apply m => Bind m where
- (-<<) :: Bind m => (a -> m b) -> m a -> m b
- (-<-) :: Bind m => (b -> m c) -> (a -> m b) -> a -> m c
- (->-) :: Bind m => (a -> m b) -> (b -> m c) -> a -> m c
- apDefault :: Bind f => f (a -> b) -> f a -> f b
- returning :: Functor f => f a -> (a -> b) -> f b
Functors
The Functor class is used for types that can be mapped over.
Instances of Functor should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor for lists, Maybe and IO
satisfy these laws.
Minimal complete definition
Instances
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
An infix synonym for fmap.
The name of this operator is an allusion to $.
Note the similarities between their types:
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b
Whereas $ is function application, <$> is function
application lifted over a Functor.
Examples
Convert from a to a Maybe Int using Maybe Stringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to an Either Int IntEither IntString using show:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "17"
Double each element of a list:
>>>(*2) <$> [1,2,3][2,4,6]
Apply even to the second element of a pair:
>>>even <$> (2,2)(2,True)
($>) :: Functor f => f a -> b -> f b infixl 4 #
Flipped version of <$.
Examples
Replace the contents of a with a constant Maybe IntString:
>>>Nothing $> "foo"Nothing>>>Just 90210 $> "foo"Just "foo"
Replace the contents of an with a constant
Either Int IntString, resulting in an :Either Int String
>>>Left 8675309 $> "foo"Left 8675309>>>Right 8675309 $> "foo"Right "foo"
Replace each element of a list with a constant String:
>>>[1,2,3] $> "foo"["foo","foo","foo"]
Replace the second element of a pair with a constant String:
>>>(1,2) $> "foo"(1,"foo")
Since: 4.7.0.0
Applyable functors
class Functor f => Apply f where Source #
A strong lax semi-monoidal endofunctor.
This is equivalent to an Applicative without pure.
Laws:
(.)<$>u<.>v<.>w = u<.>(v<.>w) x<.>(f<$>y) = (.f)<$>x<.>y f<$>(x<.>y) = (f.)<$>x<.>y
The laws imply that .> and <. really ignore their
left and right results, respectively, and really
return their right and left results, respectively.
Specifically,
(mf<$>m).>(nf<$>n) = nf<$>(m.>n) (mf<$>m)<.(nf<$>n) = mf<$>(m<.n)
Minimal complete definition
Methods
(<.>) :: f (a -> b) -> f a -> f b infixl 4 Source #
Instances
(<..>) :: Apply w => w a -> w (a -> b) -> w b infixl 4 Source #
A variant of <.> with the arguments reversed.
liftF2 :: Apply w => (a -> b -> c) -> w a -> w b -> w c Source #
Lift a binary function into a comonad with zipping
liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d Source #
Lift a ternary function into a comonad with zipping
Wrappers
newtype WrappedApplicative f a Source #
Wrap an Applicative to be used as a member of Apply
Constructors
| WrapApplicative | |
Fields
| |
Instances
| Functor f => Functor (WrappedApplicative f) Source # | |
| Applicative f => Applicative (WrappedApplicative f) Source # | |
| Alternative f => Alternative (WrappedApplicative f) Source # | |
| Applicative f => Apply (WrappedApplicative f) Source # | |
| Alternative f => Alt (WrappedApplicative f) Source # | |
| Alternative f => Plus (WrappedApplicative f) Source # | |
newtype MaybeApply f a Source #
Transform a Apply into an Applicative by adding a unit.
Constructors
| MaybeApply | |
Fields
| |
Instances
| Functor f => Functor (MaybeApply f) Source # | |
| Apply f => Applicative (MaybeApply f) Source # | |
| Comonad f => Comonad (MaybeApply f) Source # | |
| Extend f => Extend (MaybeApply f) Source # | |
| Apply f => Apply (MaybeApply f) Source # | |
Bindable functors
class Apply m => Bind m where Source #
Minimal definition: Either join or >>-
If defining both, then the following laws (the default definitions) must hold:
join = (>>- id) m >>- f = join (fmap f m)
Laws:
induced definition of <.>: f <.> x = f >>- (<$> x)
Finally, there are two associativity conditions:
associativity of (>>-): (m >>- f) >>- g == m >>- (\x -> f x >>- g) associativity of join: join . join = join . fmap join
These can both be seen as special cases of the constraint that
associativity of (->-): (f ->- g) ->- h = f ->- (g ->- h)
Instances