Copyright (C) 2008-2015 Edward Kmett(C) 2004 Dave Menendez BSD-style (see the file LICENSE) Edward Kmett provisional portable Safe Haskell2010

Description

Synopsis

class Functor w => Comonad w where Source #

There are two ways to define a comonad:

I. Provide definitions for extract and extend satisfying these laws:

extend extract      = id
extract . extend f  = f
extend f . extend g = extend (f . extend g)


In this case, you may simply set fmap = liftW.

These laws are directly analogous to the laws for monads and perhaps can be made clearer by viewing them as laws stating that Cokleisli composition must be associative, and has extract for a unit:

f =>= extract   = f
extract =>= f   = f
(f =>= g) =>= h = f =>= (g =>= h)


II. Alternately, you may choose to provide definitions for fmap, extract, and duplicate satisfying these laws:

extract . duplicate      = id
fmap extract . duplicate = id
duplicate . duplicate    = fmap duplicate . duplicate


In this case you may not rely on the ability to define fmap in terms of liftW.

You may of course, choose to define both duplicate and extend. In that case you must also satisfy these laws:

extend f  = fmap f . duplicate
duplicate = extend id
fmap f    = extend (f . extract)


These are the default definitions of extend and duplicate and the definition of liftW respectively.

Minimal complete definition

Methods

extract :: w a -> a Source #

extract . fmap f = f . extract


duplicate :: w a -> w (w a) Source #

duplicate = extend id
fmap (fmap f) . duplicate = duplicate . fmap f


extend :: (w a -> b) -> w a -> w b Source #

extend f = fmap f . duplicate


Instances

liftW :: Comonad w => (a -> b) -> w a -> w b Source #

A suitable default definition for fmap for a Comonad. Promotes a function to a comonad.

You can only safely use to define fmap if your Comonad defined extend, not just duplicate, since defining extend in terms of duplicate uses fmap!

fmap f = liftW f = extend (f . extract)


wfix :: Comonad w => w (w a -> a) -> a Source #

Comonadic fixed point à la David Menendez

cfix :: Comonad w => (w a -> a) -> w a Source #

Comonadic fixed point à la Dominic Orchard

kfix :: ComonadApply w => w (w a -> a) -> w a Source #

Comonadic fixed point à la Kenneth Foner:

This is the evaluate function from his "Getting a Quick Fix on Comonads" talk.

(=>=) :: Comonad w => (w a -> b) -> (w b -> c) -> w a -> c infixr 1 Source #

Left-to-right Cokleisli composition

(=<=) :: Comonad w => (w b -> c) -> (w a -> b) -> w a -> c infixr 1 Source #

Right-to-left Cokleisli composition

(<<=) :: Comonad w => (w a -> b) -> w a -> w b infixr 1 Source #

extend in operator form

(=>>) :: Comonad w => w a -> (w a -> b) -> w b infixl 1 Source #

extend with the arguments swapped. Dual to >>= for a Monad.

ComonadApply is to Comonad like Applicative is to Monad.

Mathematically, it is a strong lax symmetric semi-monoidal comonad on the category Hask of Haskell types. That it to say that w is a strong lax symmetric semi-monoidal functor on Hask, where both extract and duplicate are symmetric monoidal natural transformations.

Laws:

(.) <$> u <@> v <@> w = u <@> (v <@> w) extract (p <@> q) = extract p (extract q) duplicate (p <@> q) = (<@>) <$> duplicate p <@> duplicate q


If our type is both a ComonadApply and Applicative we further require

(<*>) = (<@>)


Finally, if you choose to define (<@) and (@>), the results of your definitions should match the following laws:

a @> b = const id <$> a <@> b a <@ b = const <$> a <@> b


Methods

(<@>) :: w (a -> b) -> w a -> w b infixl 4 Source #

(<@>) :: Applicative w => w (a -> b) -> w a -> w b infixl 4 Source #

(@>) :: w a -> w b -> w b infixl 4 Source #

(<@) :: w a -> w b -> w a infixl 4 Source #

Instances

 Source # Methods(<@>) :: Identity (a -> b) -> Identity a -> Identity b Source #(@>) :: Identity a -> Identity b -> Identity b Source #(<@) :: Identity a -> Identity b -> Identity a Source # Source # Methods(<@>) :: NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b Source #(@>) :: NonEmpty a -> NonEmpty b -> NonEmpty b Source #(<@) :: NonEmpty a -> NonEmpty b -> NonEmpty a Source # Source # Methods(<@>) :: Tree (a -> b) -> Tree a -> Tree b Source #(@>) :: Tree a -> Tree b -> Tree b Source #(<@) :: Tree a -> Tree b -> Tree a Source # Monoid m => ComonadApply ((->) m) Source # Methods(<@>) :: (m -> a -> b) -> (m -> a) -> m -> b Source #(@>) :: (m -> a) -> (m -> b) -> m -> b Source #(<@) :: (m -> a) -> (m -> b) -> m -> a Source # Semigroup m => ComonadApply ((,) m) Source # Methods(<@>) :: (m, a -> b) -> (m, a) -> (m, b) Source #(@>) :: (m, a) -> (m, b) -> (m, b) Source #(<@) :: (m, a) -> (m, b) -> (m, a) Source # Source # Methods(<@>) :: IdentityT * w (a -> b) -> IdentityT * w a -> IdentityT * w b Source #(@>) :: IdentityT * w a -> IdentityT * w b -> IdentityT * w b Source #(<@) :: IdentityT * w a -> IdentityT * w b -> IdentityT * w a Source # (Semigroup e, ComonadApply w) => ComonadApply (EnvT e w) Source # Methods(<@>) :: EnvT e w (a -> b) -> EnvT e w a -> EnvT e w b Source #(@>) :: EnvT e w a -> EnvT e w b -> EnvT e w b Source #(<@) :: EnvT e w a -> EnvT e w b -> EnvT e w a Source # (ComonadApply w, Semigroup s) => ComonadApply (StoreT s w) Source # Methods(<@>) :: StoreT s w (a -> b) -> StoreT s w a -> StoreT s w b Source #(@>) :: StoreT s w a -> StoreT s w b -> StoreT s w b Source #(<@) :: StoreT s w a -> StoreT s w b -> StoreT s w a Source # (ComonadApply w, Monoid m) => ComonadApply (TracedT m w) Source # Methods(<@>) :: TracedT m w (a -> b) -> TracedT m w a -> TracedT m w b Source #(@>) :: TracedT m w a -> TracedT m w b -> TracedT m w b Source #(<@) :: TracedT m w a -> TracedT m w b -> TracedT m w a Source #

(<@@>) :: ComonadApply w => w a -> w (a -> b) -> w b infixl 4 Source #

A variant of <@> with the arguments reversed.

liftW2 :: ComonadApply w => (a -> b -> c) -> w a -> w b -> w c Source #

Lift a binary function into a Comonad with zipping

liftW3 :: ComonadApply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d Source #

Lift a ternary function into a Comonad with zipping

# Cokleisli Arrows

newtype Cokleisli w a b Source #

The Cokleisli Arrows of a given Comonad

Constructors

 Cokleisli FieldsrunCokleisli :: w a -> b

Instances

 Comonad w => Arrow (Cokleisli w) Source # Methodsarr :: (b -> c) -> Cokleisli w b c #first :: Cokleisli w b c -> Cokleisli w (b, d) (c, d) #second :: Cokleisli w b c -> Cokleisli w (d, b) (d, c) #(***) :: Cokleisli w b c -> Cokleisli w b' c' -> Cokleisli w (b, b') (c, c') #(&&&) :: Cokleisli w b c -> Cokleisli w b c' -> Cokleisli w b (c, c') # Comonad w => ArrowChoice (Cokleisli w) Source # Methodsleft :: Cokleisli w b c -> Cokleisli w (Either b d) (Either c d) #right :: Cokleisli w b c -> Cokleisli w (Either d b) (Either d c) #(+++) :: Cokleisli w b c -> Cokleisli w b' c' -> Cokleisli w (Either b b') (Either c c') #(|||) :: Cokleisli w b d -> Cokleisli w c d -> Cokleisli w (Either b c) d # Comonad w => ArrowApply (Cokleisli w) Source # Methodsapp :: Cokleisli w (Cokleisli w b c, b) c # Source # Methodsloop :: Cokleisli w (b, d) (c, d) -> Cokleisli w b c # Comonad w => Category * (Cokleisli w) Source # Methodsid :: cat a a #(.) :: cat b c -> cat a b -> cat a c # Monad (Cokleisli w a) Source # Methods(>>=) :: Cokleisli w a a -> (a -> Cokleisli w a b) -> Cokleisli w a b #(>>) :: Cokleisli w a a -> Cokleisli w a b -> Cokleisli w a b #return :: a -> Cokleisli w a a #fail :: String -> Cokleisli w a a # Functor (Cokleisli w a) Source # Methodsfmap :: (a -> b) -> Cokleisli w a a -> Cokleisli w a b #(<$) :: a -> Cokleisli w a b -> Cokleisli w a a # Applicative (Cokleisli w a) Source # Methodspure :: a -> Cokleisli w a a #(<*>) :: Cokleisli w a (a -> b) -> Cokleisli w a a -> Cokleisli w a b #(*>) :: Cokleisli w a a -> Cokleisli w a b -> Cokleisli w a b #(<*) :: Cokleisli w a a -> Cokleisli w a b -> Cokleisli w a a # # 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. Minimal complete definition fmap Methods fmap :: (a -> b) -> f a -> f b # (<$) :: a -> f b -> f a infixl 4 #

Replace all locations in the input with the same value. The default definition is fmap . const, but this may be overridden with a more efficient version.

Instances

 Functor [] Methodsfmap :: (a -> b) -> [a] -> [b] #(<$) :: a -> [b] -> [a] # Methodsfmap :: (a -> b) -> Maybe a -> Maybe b #(<$) :: a -> Maybe b -> Maybe a # Methodsfmap :: (a -> b) -> IO a -> IO b #(<$) :: a -> IO b -> IO a # Methodsfmap :: (a -> b) -> V1 a -> V1 b #(<$) :: a -> V1 b -> V1 a # Methodsfmap :: (a -> b) -> U1 a -> U1 b #(<$) :: a -> U1 b -> U1 a # Methodsfmap :: (a -> b) -> Par1 a -> Par1 b #(<$) :: a -> Par1 b -> Par1 a # Functor Id Methodsfmap :: (a -> b) -> Id a -> Id b #(<$) :: a -> Id b -> Id a # Methodsfmap :: (a -> b) -> Identity a -> Identity b #(<$) :: a -> Identity b -> Identity a # Methodsfmap :: (a -> b) -> Min a -> Min b #(<$) :: a -> Min b -> Min a # Methodsfmap :: (a -> b) -> Max a -> Max b #(<$) :: a -> Max b -> Max a # Methodsfmap :: (a -> b) -> First a -> First b #(<$) :: a -> First b -> First a # Methodsfmap :: (a -> b) -> Last a -> Last b #(<$) :: a -> Last b -> Last a # Methodsfmap :: (a -> b) -> Option a -> Option b #(<$) :: a -> Option b -> Option a # Methodsfmap :: (a -> b) -> NonEmpty a -> NonEmpty b #(<$) :: a -> NonEmpty b -> NonEmpty a # Methodsfmap :: (a -> b) -> Complex a -> Complex b #(<$) :: a -> Complex b -> Complex a # Methodsfmap :: (a -> b) -> ZipList a -> ZipList b #(<$) :: a -> ZipList b -> ZipList a # Methodsfmap :: (a -> b) -> Dual a -> Dual b #(<$) :: a -> Dual b -> Dual a # Methodsfmap :: (a -> b) -> Sum a -> Sum b #(<$) :: a -> Sum b -> Sum a # Methodsfmap :: (a -> b) -> Product a -> Product b #(<$) :: a -> Product b -> Product a # Methodsfmap :: (a -> b) -> First a -> First b #(<$) :: a -> First b -> First a # Methodsfmap :: (a -> b) -> Last a -> Last b #(<$) :: a -> Last b -> Last a # Functor Digit Methodsfmap :: (a -> b) -> Digit a -> Digit b #(<$) :: a -> Digit b -> Digit a # Functor Node Methodsfmap :: (a -> b) -> Node a -> Node b #(<$) :: a -> Node b -> Node a # Functor Elem Methodsfmap :: (a -> b) -> Elem a -> Elem b #(<$) :: a -> Elem b -> Elem a # Functor FingerTree Methodsfmap :: (a -> b) -> FingerTree a -> FingerTree b #(<$) :: a -> FingerTree b -> FingerTree a # Methodsfmap :: (a -> b) -> Tree a -> Tree b #(<$) :: a -> Tree b -> Tree a # Methodsfmap :: (a -> b) -> Seq a -> Seq b #(<$) :: a -> Seq b -> Seq a # Methodsfmap :: (a -> b) -> ViewL a -> ViewL b #(<$) :: a -> ViewL b -> ViewL a # Methodsfmap :: (a -> b) -> ViewR a -> ViewR b #(<$) :: a -> ViewR b -> ViewR a # Functor ((->) r) Methodsfmap :: (a -> b) -> (r -> a) -> r -> b #(<$) :: a -> (r -> b) -> r -> a # Methodsfmap :: (a -> b) -> Either a a -> Either a b #(<$) :: a -> Either a b -> Either a a # Functor f => Functor (Rec1 f) Methodsfmap :: (a -> b) -> Rec1 f a -> Rec1 f b #(<$) :: a -> Rec1 f b -> Rec1 f a # Methodsfmap :: (a -> b) -> URec Char a -> URec Char b #(<$) :: a -> URec Char b -> URec Char a # Methodsfmap :: (a -> b) -> URec Double a -> URec Double b #(<$) :: a -> URec Double b -> URec Double a # Methodsfmap :: (a -> b) -> URec Float a -> URec Float b #(<$) :: a -> URec Float b -> URec Float a # Methodsfmap :: (a -> b) -> URec Int a -> URec Int b #(<$) :: a -> URec Int b -> URec Int a # Methodsfmap :: (a -> b) -> URec Word a -> URec Word b #(<$) :: a -> URec Word b -> URec Word a # Functor (URec (Ptr ())) Methodsfmap :: (a -> b) -> URec (Ptr ()) a -> URec (Ptr ()) b #(<$) :: a -> URec (Ptr ()) b -> URec (Ptr ()) a # Functor ((,) a) Methodsfmap :: (a -> b) -> (a, a) -> (a, b) #(<$) :: a -> (a, b) -> (a, a) # Functor (StateL s) Methodsfmap :: (a -> b) -> StateL s a -> StateL s b #(<$) :: a -> StateL s b -> StateL s a # Functor (StateR s) Methodsfmap :: (a -> b) -> StateR s a -> StateR s b #(<$) :: a -> StateR s b -> StateR s a # Functor (Arg a) Methodsfmap :: (a -> b) -> Arg a a -> Arg a b #(<$) :: a -> Arg a b -> Arg a a # Monad m => Functor (WrappedMonad m) Methodsfmap :: (a -> b) -> WrappedMonad m a -> WrappedMonad m b #(<$) :: a -> WrappedMonad m b -> WrappedMonad m a # Arrow a => Functor (ArrowMonad a) Methodsfmap :: (a -> b) -> ArrowMonad a a -> ArrowMonad a b #(<$) :: a -> ArrowMonad a b -> ArrowMonad a a # Methodsfmap :: (a -> b) -> Proxy * a -> Proxy * b #(<$) :: a -> Proxy * b -> Proxy * a # Functor (State s) Methodsfmap :: (a -> b) -> State s a -> State s b #(<$) :: a -> State s b -> State s a # Functor (K1 i c) Methodsfmap :: (a -> b) -> K1 i c a -> K1 i c b #(<$) :: a -> K1 i c b -> K1 i c a # (Functor g, Functor f) => Functor ((:+:) f g) Methodsfmap :: (a -> b) -> (f :+: g) a -> (f :+: g) b #(<$) :: a -> (f :+: g) b -> (f :+: g) a # (Functor g, Functor f) => Functor ((:*:) f g) Methodsfmap :: (a -> b) -> (f :*: g) a -> (f :*: g) b #(<$) :: a -> (f :*: g) b -> (f :*: g) a # (Functor g, Functor f) => Functor ((:.:) f g) Methodsfmap :: (a -> b) -> (f :.: g) a -> (f :.: g) b #(<$) :: a -> (f :.: g) b -> (f :.: g) a # Arrow a => Functor (WrappedArrow a b) Methodsfmap :: (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b #(<$) :: a -> WrappedArrow a b b -> WrappedArrow a b a # Methodsfmap :: (a -> b) -> Const * m a -> Const * m b #(<$) :: a -> Const * m b -> Const * m a # Functor f => Functor (Alt * f) Methodsfmap :: (a -> b) -> Alt * f a -> Alt * f b #(<$) :: a -> Alt * f b -> Alt * f a # Functor (Tagged k s) Methodsfmap :: (a -> b) -> Tagged k s a -> Tagged k s b #(<$) :: a -> Tagged k s b -> Tagged k s a # Functor m => Functor (IdentityT * m) Methodsfmap :: (a -> b) -> IdentityT * m a -> IdentityT * m b #(<$) :: a -> IdentityT * m b -> IdentityT * m a # Functor (Cokleisli w a) # Methodsfmap :: (a -> b) -> Cokleisli w a a -> Cokleisli w a b #(<$) :: a -> Cokleisli w a b -> Cokleisli w a a # Functor w => Functor (EnvT e w) # Methodsfmap :: (a -> b) -> EnvT e w a -> EnvT e w b #(<$) :: a -> EnvT e w b -> EnvT e w a # Functor w => Functor (StoreT s w) # Methodsfmap :: (a -> b) -> StoreT s w a -> StoreT s w b #(<$) :: a -> StoreT s w b -> StoreT s w a # Functor w => Functor (TracedT m w) # Methodsfmap :: (a -> b) -> TracedT m w a -> TracedT m w b #(<$) :: a -> TracedT m w b -> TracedT m w a # Functor f => Functor (M1 i c f) Methodsfmap :: (a -> b) -> M1 i c f a -> M1 i c f b #(<$) :: a -> M1 i c f b -> M1 i c f a # (Functor f, Functor g) => Functor (Sum * f g) Methodsfmap :: (a -> b) -> Sum * f g a -> Sum * f g b #(<$) :: a -> Sum * f g b -> Sum * f g a # (Functor f, Functor g) => Functor (Product * f g) Methodsfmap :: (a -> b) -> Product * f g a -> Product * f g b #(<$) :: a -> Product * f g b -> Product * f g a # (Functor f, Functor g) => Functor (Compose * * f g) Methodsfmap :: (a -> b) -> Compose * * f g a -> Compose * * f g b #(<$) :: a -> Compose * * f g b -> Compose * * f g a # (<$>) :: 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 Maybe Int to a Maybe String using show: >>> show <$> Nothing
Nothing
>>> show <$> Just 3 Just "3"  Convert from an Either Int Int to an 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 Maybe Int with a constant String: >>> Nothing$> "foo"
Nothing
>>> Just 90210 $> "foo" Just "foo"  Replace the contents of an Either Int Int with a constant 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: 4.7.0.0