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 |
Synopsis
- 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
- 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
class Functor (f :: * -> *) where #
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.
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
String
show
:
>>>
show <$> Nothing
Nothing>>>
show <$> Just 3
Just "3"
Convert from an
to an Either
Int
Int
Either
Int
String
using show
:
>>>
show <$> Left 17
Left 17>>>
show <$> Right 17
Right "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
Int
String
:
>>>
Nothing $> "foo"
Nothing>>>
Just 90210 $> "foo"
Just "foo"
Replace the contents of an
with a constant
Either
Int
Int
String
, 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: base-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)
(<.>) :: f (a -> b) -> f a -> f b infixl 4 Source #
(.>) :: f a -> f b -> f b infixl 4 Source #
(<.) :: f a -> f b -> f a infixl 4 Source #
liftF2 :: (a -> b -> c) -> f a -> f b -> f c Source #
Lift a binary function into a comonad with zipping
Instances
(<..>) :: Apply w => w a -> w (a -> b) -> w b infixl 4 Source #
A variant of <.>
with the arguments reversed.
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
WrapApplicative | |
|
Instances
newtype MaybeApply f a Source #
Transform a Apply into an Applicative by adding a unit.
MaybeApply | |
|
Instances
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)