day-comonoid-0.1: A comonoid w.r.t. Day
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Functor.Day.Comonoid

Synopsis

Comonoid type class

class Comonad f => Comonoid f where Source #

Comonoid with respect to Day convolution.

Laws

coapply must satisfy the following equations. Here, erase1 and erase2 are defined using extract method inherited from Comonad.

erase1 . coapply = id
erase2 . coapply = id
trans1 coapply . coapply = assoc . trans2 coapply . coapply

Furthermore, duplicateDefault derived from coapply must be equivalent to duplicate inherited from Comonad.

duplicateDefault = dayToCompose . coapply
                 = duplicate

Examples

Env comonad, or (,) e, is an instance of Comonoid.

instance Comonoid ((,) e) where
  coapply :: forall x. (e, x) -> Day ((,) e) ((,) e) x
  -- ~ forall x. (e,x) -> ∃b c. ((e,b), (e,c), b -> c -> x)
  -- ~ forall x. (e,x) -> (e,e, ∃b c.(b, c, b -> c -> x))
  -- ~ forall x. (e,x) -> (e,e,x)
  -- ~ e -> (e,e)
  coapply (e, x) = Day (e, ()) (e, ()) (\_ _ -> x)

Traced comonad, or ((->) m), is another example.

instance Monoid m => Comonoid ((->) m) where
  coapply :: forall x. (m -> x) -> Day ((->) m) ((->) m) x
  -- ~ forall x. (m -> x) -> ∃b c. (m -> b, m -> c, b -> c -> x)
  -- ~ forall x. (m -> x) -> (m -> m -> x)
  -- ~ m -> m -> m
  coapply f = Day id id (\x y -> f (x <> y))

Non-example

Unlike Env or Traced, Store comonad can't be an instance of Comonoid. The law requires any lawful Comonoid f to satisfy the following property.

  • For any value of f x, coapply doesn't change the "shape" of it. Precisely, for any value fx :: f x, the following equation is true.

    () <$ coapply fx ≡ Day (() <$ fx) (() <$ fx) (\_ _ -> ())@

Therefore, any lawful Comonoid (Store s) must satisfy the following equation:

coapply (store u s0) ≡ Day (store u s0) (store u s0) (\_ _ -> ())
  where u = const () :: s -> ()

But it's incompatible with another requirement that duplicateDefault must be equivalent to duplicate of the Comonad (Store s) instance.

duplicateDefault (store u s0) = store (const (store u s0)) s0
duplicate        (store u s0) = store (\s1 -> store u s1)  s0

Methods

coapply :: f a -> Day f f a Source #

Instances

Instances details
Comonoid Identity Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Comonoid ((,) e) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: (e, a) -> Day ((,) e) ((,) e) a Source #

Comonoid f => Comonoid (EnvT e f) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: EnvT e f a -> Day (EnvT e f) (EnvT e f) a Source #

(Monoid m, Comonoid f) => Comonoid (TracedT m f) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: TracedT m f a -> Day (TracedT m f) (TracedT m f) a Source #

(Comonoid f, Comonoid g) => Comonoid (Day f g) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: Day f g a -> Day (Day f g) (Day f g) a Source #

Comonoid f => Comonoid (IdentityT f) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: IdentityT f a -> Day (IdentityT f) (IdentityT f) a Source #

(Comonoid f, Comonoid g) => Comonoid (Sum f g) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: Sum f g a -> Day (Sum f g) (Sum f g) a Source #

Monoid m => Comonoid ((->) m) Source # 
Instance details

Defined in Data.Functor.Day.Comonoid

Methods

coapply :: (m -> a) -> Day ((->) m) ((->) m) a Source #

erase1 :: (Comonad f, Functor g) => Day f g c -> g c Source #

erase1 = elim1 . trans1 (Identity . extract)

erase2 :: (Functor f, Comonad g) => Day f g c -> f c Source #

erase2 = elim2 . trans2 (Identity . extract)

duplicateDefault :: Comonoid f => f a -> f (f a) Source #

Every Comonoid is a Comonad.

extendDefault :: Comonoid f => (f a -> b) -> f a -> f b Source #

Every Comonoid is a Comonad.

dayToCompose :: (Functor f, Functor g) => Day f g a -> f (g a) Source #

Day f g can be turned into a composition of f and g.

Re-export

class Functor w => Comonad (w :: Type -> Type) where #

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 #

extract . fmap f = f . extract

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

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

Instances

Instances details
Comonad Identity 
Instance details

Defined in Control.Comonad

Methods

extract :: Identity a -> a #

duplicate :: Identity a -> Identity (Identity a) #

extend :: (Identity a -> b) -> Identity a -> Identity b #

Comonad Tree 
Instance details

Defined in Control.Comonad

Methods

extract :: Tree a -> a #

duplicate :: Tree a -> Tree (Tree a) #

extend :: (Tree a -> b) -> Tree a -> Tree b #

Comonad NonEmpty 
Instance details

Defined in Control.Comonad

Methods

extract :: NonEmpty a -> a #

duplicate :: NonEmpty a -> NonEmpty (NonEmpty a) #

extend :: (NonEmpty a -> b) -> NonEmpty a -> NonEmpty b #

Comonad (Arg e) 
Instance details

Defined in Control.Comonad

Methods

extract :: Arg e a -> a #

duplicate :: Arg e a -> Arg e (Arg e a) #

extend :: (Arg e a -> b) -> Arg e a -> Arg e b #

Comonad ((,) e) 
Instance details

Defined in Control.Comonad

Methods

extract :: (e, a) -> a #

duplicate :: (e, a) -> (e, (e, a)) #

extend :: ((e, a) -> b) -> (e, a) -> (e, b) #

Comonad w => Comonad (EnvT e w) 
Instance details

Defined in Control.Comonad.Trans.Env

Methods

extract :: EnvT e w a -> a #

duplicate :: EnvT e w a -> EnvT e w (EnvT e w a) #

extend :: (EnvT e w a -> b) -> EnvT e w a -> EnvT e w b #

(Comonad w, Monoid m) => Comonad (TracedT m w) 
Instance details

Defined in Control.Comonad.Trans.Traced

Methods

extract :: TracedT m w a -> a #

duplicate :: TracedT m w a -> TracedT m w (TracedT m w a) #

extend :: (TracedT m w a -> b) -> TracedT m w a -> TracedT m w b #

(Comonad f, Comonad g) => Comonad (Day f g) 
Instance details

Defined in Data.Functor.Day

Methods

extract :: Day f g a -> a #

duplicate :: Day f g a -> Day f g (Day f g a) #

extend :: (Day f g a -> b) -> Day f g a -> Day f g b #

Comonad (Tagged s) 
Instance details

Defined in Control.Comonad

Methods

extract :: Tagged s a -> a #

duplicate :: Tagged s a -> Tagged s (Tagged s a) #

extend :: (Tagged s a -> b) -> Tagged s a -> Tagged s b #

Comonad w => Comonad (IdentityT w) 
Instance details

Defined in Control.Comonad

Methods

extract :: IdentityT w a -> a #

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

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

(Comonad f, Comonad g) => Comonad (Sum f g) 
Instance details

Defined in Control.Comonad

Methods

extract :: Sum f g a -> a #

duplicate :: Sum f g a -> Sum f g (Sum f g a) #

extend :: (Sum f g a -> b) -> Sum f g a -> Sum f g b #

Monoid m => Comonad ((->) m) 
Instance details

Defined in Control.Comonad

Methods

extract :: (m -> a) -> a #

duplicate :: (m -> a) -> m -> (m -> a) #

extend :: ((m -> a) -> b) -> (m -> a) -> m -> b #