Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created December 1, 2012 15:13
Show Gist options
  • Save michaelficarra/4182825 to your computer and use it in GitHub Desktop.
Save michaelficarra/4182825 to your computer and use it in GitHub Desktop.
import Control.Applicative ((<*>))
import Control.Parallel (par)
class Comonad w where
extend :: (w a -> b) -> w a -> w b
duplicate :: w a -> w (w a)
extract :: w a -> a
-- if monads are monoids on functions which produce side effects,
--
-- class MonadAsMonoid m where
-- mappend :: (a -> m b) -> (b -> m c) -> a -> m c
-- mempty :: a -> m a
--
-- then comonads are monoids on functions which consume side effects
class ComonadAsMonoid w where
mappend :: (w a -> b) -> (w b -> c) -> w a -> c
mempty :: w a -> a
mconcat :: [w a -> a] -> w a -> a
mconcat = foldr mappend mempty
newtype Future a = Future { await :: a }
future = par <*> Future
duplicate :: Future a -> Future (Future a)
instance Comonad Future where
extend = (.) Future
duplicate = Future
extract = await
instance ComonadAsMonoid Future where
mappend f g = g . Future . f
mempty = await
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment