Skip to content

Instantly share code, notes, and snippets.

@raichoo
Created July 16, 2015 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raichoo/f1ecaff3a465bbbd797b to your computer and use it in GitHub Desktop.
Save raichoo/f1ecaff3a465bbbd797b to your computer and use it in GitHub Desktop.
Comonad: Dual of a Monad
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RebindableSyntax #-}
module Comonad where
import Prelude (Functor(..))
class Functor m => Monad m where
return :: a -> m a
join :: m (m a) -> m a
infixl 1 >>=
(>>=) :: Monad m => m a -> (a -> m b) -> m b
m >>= f = join (fmap f m)
class Functor w => Comonad w where
extract :: w a -> a
duplicate :: w a -> w (w a)
infixl 1 =>>
(=>>) :: Comonad w => w a -> (w a -> b) -> w b
w =>> f = fmap f (duplicate w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment