comonad-5: Comonads

Copyright(C) 2008-2015 Edward Kmett, (C) 2004 Dave Menendez
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Control.Comonad

Contents

Description

 

Synopsis

Comonads

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

extract, (duplicate | extend)

Methods

extract :: w a -> a Source

extract . fmap f = f . extract

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

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

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.

Combining Comonads

class Comonad w => ComonadApply w where Source

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

Minimal complete definition

Nothing

Methods

(<@>) :: 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

(<@@>) :: 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

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 f => (a -> b) -> f a -> f b infixl 4

An infix synonym for fmap.

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