| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.Functor.Day.Comonoid
Contents
Synopsis
- class Comonad f => Comonoid f where
- erase1 :: (Comonad f, Functor g) => Day f g c -> g c
- erase2 :: (Functor f, Comonad g) => Day f g c -> f c
- duplicateDefault :: Comonoid f => f a -> f (f a)
- extendDefault :: Comonoid f => (f a -> b) -> f a -> f b
- dayToCompose :: (Functor f, Functor g) => Day f g a -> f (g a)
- class Functor w => Comonad (w :: Type -> Type) where
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= iderase2.coapply= idtrans1coapply.coapply=assoc.trans2coapply.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,coapplydoesn't change the "shape" of it. Precisely, for any valuefx :: 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
Instances
| Comonoid Identity Source # | |
| Comonoid ((,) e) Source # | |
| Comonoid f => Comonoid (EnvT e f) Source # | |
| (Monoid m, Comonoid f) => Comonoid (TracedT m f) Source # | |
| (Comonoid f, Comonoid g) => Comonoid (Day f g) Source # | |
| Comonoid f => Comonoid (IdentityT f) Source # | |
| (Comonoid f, Comonoid g) => Comonoid (Sum f g) Source # | |
| Monoid m => Comonoid ((->) m) Source # | |
Defined in Data.Functor.Day.Comonoid | |
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)
dayToCompose :: (Functor f, Functor g) => Day f g a -> f (g a) Source #
can be turned into a composition of Day f gf 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:
extendextract=idextract.extendf = fextendf .extendg =extend(f .extendg)
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= fextract=>=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=idfmapextract.duplicate=idduplicate.duplicate=fmapduplicate.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:
extendf =fmapf .duplicateduplicate=extendidfmapf =extend(f .extract)
These are the default definitions of extend and duplicate and
the definition of liftW respectively.
Methods
Instances
| Comonad Identity | |
| Comonad Tree | |
| Comonad NonEmpty | |
| Comonad (Arg e) | |
| Comonad ((,) e) | |
| Comonad w => Comonad (EnvT e w) | |
| (Comonad w, Monoid m) => Comonad (TracedT m w) | |
| (Comonad f, Comonad g) => Comonad (Day f g) | |
| Comonad (Tagged s) | |
| Comonad w => Comonad (IdentityT w) | |
| (Comonad f, Comonad g) => Comonad (Sum f g) | |
| Monoid m => Comonad ((->) m) | |