0 | module Control.Comonad
2 | import Control.Monad.Identity
5 | import Data.Morphisms
15 | interface Functor w => Comonad w where
18 | duplicate : w a -> w (w a)
19 | duplicate = extend id
21 | extend : (w a -> b) -> w a -> w b
22 | extend f = map f . duplicate
29 | Comonad (Pair e) where
31 | duplicate (e,a) = (e, (e, a))
34 | Comonad Identity where
35 | extract = runIdentity
39 | Comonad Stream where
41 | duplicate xs@(_ :: t) = xs :: duplicate t
46 | duplicate (x ::: xs) = (x ::: xs) ::: tails xs
49 | tails : List a -> List (List1 a)
51 | tails (x :: xs) = (x ::: xs) :: tails xs
54 | Monoid e => Comonad (Morphism e) where
55 | extract (Mor f) = f neutral
56 | duplicate (Mor f) = Mor $
\e => Mor (\e' => f (e <+> e'))
62 | export infixl 4 <@
, @>
, <@@>
, <@>
65 | interface Comonad w => ComonadApply w where
66 | (<@>) : w (a -> b) -> w a -> w b
68 | (@>) : w a -> w b -> w b
71 | (<@) : w a -> w b -> w a
72 | a <@ b = map const a <@> b
79 | Semigroup m => ComonadApply (Pair m) where
80 | (m, f) <@> (n, a) = (m <+> n, f a)
81 | (m, a) <@ (n, _) = (m <+> n, a)
82 | (m, _) @> (n, b) = (m <+> n, b)
85 | ComonadApply List1 where
89 | ComonadApply Stream where
93 | Monoid m => ComonadApply (Morphism m) where
97 | ComonadApply Identity where
106 | export infixl 1 =>>
107 | export infixr 1 <<=
, =<=
, =>=
110 | public export %inline
111 | (=>>) : Comonad w => w a -> (w a -> b) -> w b
112 | (=>>) = flip extend
115 | public export %inline
116 | (<<=) : Comonad w => (w a -> b) -> w a -> w b
120 | public export %inline
121 | (=<=) : Comonad w => (w b -> c) -> (w a -> b) -> w a -> c
122 | f =<= g = f . extend g
125 | public export %inline
126 | (=>=) : Comonad w => (w a -> b) -> (w b -> c) -> w a -> c
127 | f =>= g = g . extend f
130 | public export %inline
131 | (<@@>) : ComonadApply w => w a -> w (a -> b) -> w b
132 | (<@@>) = flip (<@>)