Safe Haskell | None |
---|---|
Language | Haskell2010 |
Functor
Synopsis
- class Functor (f :: * -> *) where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- ($>) :: Functor f => f a -> b -> f b
- (<&>) :: Functor f => f a -> (a -> b) -> f b
- void :: Functor f => f a -> f ()
- unzip :: Functor f => f (a, b) -> (f a, f b)
- mapped :: Functor f => Setter (f a) (f b) a b
- class Functor f => Apply (f :: * -> *) where
- liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d
- newtype MaybeApply (f :: * -> *) a = MaybeApply {
- runMaybeApply :: Either (f a) a
- newtype Static (f :: * -> *) a b = Static {
- runStatic :: f (a -> b)
- class Functor f => Alt (f :: * -> *) where
- class Alt f => Plus (f :: * -> *) where
- class Apply m => Bind (m :: * -> *) where
- (-<<) :: Bind m => (a -> m b) -> m a -> m b
- class Functor w => Extend (w :: * -> *) where
- class Bifunctor (p :: * -> * -> *) where
- class Bifunctor p => Swapped (p :: * -> * -> *) where
- class Bifunctor p => Biapply (p :: * -> * -> *) where
- class MFunctor (t :: (* -> *) -> k -> *) where
Functor
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.
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
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
void :: Functor f => f a -> f () #
discards or ignores the result of evaluation, such
as the return value of an void
valueIO
action.
Examples
Replace the contents of a
with unit:Maybe
Int
>>>
void Nothing
Nothing>>>
void (Just 3)
Just ()
Replace the contents of an
with unit,
resulting in an Either
Int
Int
:Either
Int
'()'
>>>
void (Left 8675309)
Left 8675309>>>
void (Right 8675309)
Right ()
Replace every element of a list with unit:
>>>
void [1,2,3]
[(),(),()]
Replace the second element of a pair with unit:
>>>
void (1,2)
(1,())
Discard the result of an IO
action:
>>>
mapM print [1,2]
1 2 [(),()]>>>
void $ mapM print [1,2]
1 2
Optics
mapped :: Functor f => Setter (f a) (f b) a b #
This Setter
can be used to map over all of the values in a Functor
.
fmap
≡over
mapped
fmapDefault
≡over
traverse
(<$
) ≡set
mapped
>>>
over mapped f [a,b,c]
[f a,f b,f c]
>>>
over mapped (+1) [1,2,3]
[2,3,4]
>>>
set mapped x [a,b,c]
[x,x,x]
>>>
[[a,b],[c]] & mapped.mapped +~ x
[[a + x,b + x],[c + x]]
>>>
over (mapped._2) length [("hello","world"),("leaders","!!!")]
[("hello",5),("leaders",3)]
mapped
::Functor
f =>Setter
(f a) (f b) a b
If you want an IndexPreservingSetter
use
.setting
fmap
Apply
class Functor f => Apply (f :: * -> *) where #
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)
Methods
(<.>) :: f (a -> b) -> f a -> f b infixl 4 #
(.>) :: f a -> f b -> f b infixl 4 #
(<.) :: f a -> f b -> f a infixl 4 #
liftF2 :: (a -> b -> c) -> f a -> f b -> f c #
Lift a binary function into a comonad with zipping
Instances
liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d #
Lift a ternary function into a comonad with zipping
Newtypes
newtype MaybeApply (f :: * -> *) a #
Transform a Apply into an Applicative by adding a unit.
Constructors
MaybeApply | |
Fields
|
Instances
newtype Static (f :: * -> *) a b #
Instances
Applicative f => Arrow (Static f) | |
Defined in Data.Semigroupoid.Static | |
Alternative f => ArrowZero (Static f) | |
Defined in Data.Semigroupoid.Static | |
Alternative f => ArrowPlus (Static f) | |
Applicative f => ArrowChoice (Static f) | |
Defined in Data.Semigroupoid.Static | |
Applicative f => Category (Static f :: * -> * -> *) | |
Apply f => Semigroupoid (Static f :: * -> * -> *) | |
Functor f => Functor (Static f a) | |
Applicative f => Applicative (Static f a) | |
Defined in Data.Semigroupoid.Static | |
(Comonad f, Monoid a) => Comonad (Static f a) | |
Apply f => Apply (Static f a) | |
Pointed m => Pointed (Static m a) | |
Defined in Data.Pointed | |
Plus f => Plus (Static f a) | |
Defined in Data.Semigroupoid.Static | |
Alt f => Alt (Static f a) | |
(Extend f, Semigroup a) => Extend (Static f a) | |
Wrapped (Static f a b) | |
t ~ Static f' a' b' => Rewrapped (Static f a b) t | |
Defined in Control.Lens.Wrapped | |
type Unwrapped (Static f a b) | |
Defined in Control.Lens.Wrapped |
Alt
class Functor f => Alt (f :: * -> *) 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 distributon" law of
MonadPlus with respect to <.>
:
<.> right-distributes over <!>: (a <!> b) <.> c = (a <.> c) <!> (b <.> c)
But Maybe
, IO
,
, Either
a
, and ErrorT
e mSTM
satisfy the alternative
"left catch" law instead:
pure a <!> b = pure a
However, this variation 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
Instances
Plus
class Alt f => Plus (f :: * -> *) where #
Minimal complete definition
Instances
Bind
class Apply m => Bind (m :: * -> *) where #
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
Extend
class Functor w => Extend (w :: * -> *) where #
Minimal complete definition
Methods
duplicated :: w a -> w (w a) #
duplicated = extended id fmap (fmap f) . duplicated = duplicated . fmap f
extended :: (w a -> b) -> w a -> w b #
extended f = fmap f . duplicated
Instances
Bifunctor
class Bifunctor (p :: * -> * -> *) where #
A bifunctor is a type constructor that takes
two type arguments and is a functor in both arguments. That
is, unlike with Functor
, a type constructor such as Either
does not need to be partially applied for a Bifunctor
instance, and the methods in this class permit mapping
functions over the Left
value or the Right
value,
or both at the same time.
Formally, the class Bifunctor
represents a bifunctor
from Hask
-> Hask
.
Intuitively it is a bifunctor where both the first and second arguments are covariant.
You can define a Bifunctor
by either defining bimap
or by
defining both first
and second
.
If you supply bimap
, you should ensure that:
bimap
id
id
≡id
If you supply first
and second
, ensure:
first
id
≡id
second
id
≡id
If you supply both, you should also ensure:
bimap
f g ≡first
f.
second
g
These ensure by parametricity:
bimap
(f.
g) (h.
i) ≡bimap
f h.
bimap
g ifirst
(f.
g) ≡first
f.
first
gsecond
(f.
g) ≡second
f.
second
g
Since: base-4.8.0.0
Methods
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d #
Map over both arguments at the same time.
bimap
f g ≡first
f.
second
g
Examples
>>>
bimap toUpper (+1) ('j', 3)
('J',4)
>>>
bimap toUpper (+1) (Left 'j')
Left 'J'
>>>
bimap toUpper (+1) (Right 3)
Right 4
Instances
Bifunctor Either | Since: base-4.8.0.0 |
Bifunctor (,) | Since: base-4.8.0.0 |
Bifunctor Arg | Since: base-4.9.0.0 |
Bifunctor Gr | |
Bifunctor Entry | |
Bifunctor ((,,) x1) | Since: base-4.8.0.0 |
Bifunctor (Const :: * -> * -> *) | Since: base-4.8.0.0 |
Functor f => Bifunctor (FreeF f) | |
Functor f => Bifunctor (CofreeF f) | |
Bifunctor (Tagged :: * -> * -> *) | |
Bifunctor (K1 i :: * -> * -> *) | Since: base-4.9.0.0 |
Bifunctor ((,,,) x1 x2) | Since: base-4.8.0.0 |
Bifunctor ((,,,,) x1 x2 x3) | Since: base-4.8.0.0 |
Bifunctor p => Bifunctor (WrappedBifunctor p) | |
Defined in Data.Bifunctor.Wrapped Methods bimap :: (a -> b) -> (c -> d) -> WrappedBifunctor p a c -> WrappedBifunctor p b d # first :: (a -> b) -> WrappedBifunctor p a c -> WrappedBifunctor p b c # second :: (b -> c) -> WrappedBifunctor p a b -> WrappedBifunctor p a c # | |
Functor g => Bifunctor (Joker g :: * -> * -> *) | |
Bifunctor p => Bifunctor (Flip p) | |
Functor f => Bifunctor (Clown f :: * -> * -> *) | |
Bifunctor ((,,,,,) x1 x2 x3 x4) | Since: base-4.8.0.0 |
(Bifunctor p, Bifunctor q) => Bifunctor (Sum p q) | |
(Bifunctor f, Bifunctor g) => Bifunctor (Product f g) | |
Bifunctor ((,,,,,,) x1 x2 x3 x4 x5) | Since: base-4.8.0.0 |
(Functor f, Bifunctor p) => Bifunctor (Tannen f p) | |
(Bifunctor p, Functor f, Functor g) => Bifunctor (Biff p f g) | |
class Bifunctor p => Swapped (p :: * -> * -> *) where #
This class provides for symmetric bifunctors.
Minimal complete definition
Methods
swapped :: (Profunctor p, Functor f) => p (p b a) (f (p d c)) -> p (p a b) (f (p c d)) #
Instances
Swapped Either | |
Defined in Control.Lens.Iso | |
Swapped (,) | |
Defined in Control.Lens.Iso Methods swapped :: (Profunctor p, Functor f) => p (b, a) (f (d, c)) -> p (a, b) (f (c, d)) # |
Biapply
class Bifunctor p => Biapply (p :: * -> * -> *) where #
Minimal complete definition
Methods
(<<.>>) :: p (a -> b) (c -> d) -> p a c -> p b d infixl 4 #
Instances
Biapply (,) | |
Biapply Arg | |
Semigroup x => Biapply ((,,) x) | |
Biapply (Const :: * -> * -> *) | |
Biapply (Tagged :: * -> * -> *) | |
(Semigroup x, Semigroup y) => Biapply ((,,,) x y) | |
(Semigroup x, Semigroup y, Semigroup z) => Biapply ((,,,,) x y z) | |
Biapply p => Biapply (WrappedBifunctor p) | |
Defined in Data.Functor.Bind.Class Methods (<<.>>) :: WrappedBifunctor p (a -> b) (c -> d) -> WrappedBifunctor p a c -> WrappedBifunctor p b d # (.>>) :: WrappedBifunctor p a b -> WrappedBifunctor p c d -> WrappedBifunctor p c d # (<<.) :: WrappedBifunctor p a b -> WrappedBifunctor p c d -> WrappedBifunctor p a b # | |
Apply g => Biapply (Joker g :: * -> * -> *) | |
Biapply p => Biapply (Flip p) | |
Apply f => Biapply (Clown f :: * -> * -> *) | |
(Biapply p, Biapply q) => Biapply (Product p q) | |
(Apply f, Biapply p) => Biapply (Tannen f p) | |
(Biapply p, Apply f, Apply g) => Biapply (Biff p f g) | |
MFunctor
class MFunctor (t :: (* -> *) -> k -> *) where #
A functor in the category of monads, using hoist
as the analog of fmap
:
hoist (f . g) = hoist f . hoist g hoist id = id
Minimal complete definition
Methods
hoist :: Monad m => (forall a. m a -> n a) -> t m b -> t n b #
Lift a monad morphism from m
to n
into a monad morphism from
(t m)
to (t n)
The first argument to hoist
must be a monad morphism, even though the
type system does not enforce this
Instances
MFunctor MaybeT | |
MFunctor ListT | |
MFunctor Lift | |
MFunctor (IdentityT :: (* -> *) -> * -> *) | |
MFunctor (ExceptT e :: (* -> *) -> * -> *) | |
MFunctor (ErrorT e :: (* -> *) -> * -> *) | |
MFunctor (Backwards :: (* -> *) -> * -> *) | |
MFunctor (WriterT w :: (* -> *) -> * -> *) | |
MFunctor (StateT s :: (* -> *) -> * -> *) | |
MFunctor (StateT s :: (* -> *) -> * -> *) | |
MFunctor (WriterT w :: (* -> *) -> * -> *) | |
MFunctor (Product f :: (* -> *) -> * -> *) | |
MFunctor (ReaderT r :: (* -> *) -> * -> *) | |
Functor f => MFunctor (Compose f :: (* -> *) -> * -> *) | |
MFunctor (RWST r w s :: (* -> *) -> * -> *) | |
MFunctor (RWST r w s :: (* -> *) -> * -> *) | |