Skip to content

Instantly share code, notes, and snippets.

@barrucadu
Created March 24, 2015 23:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barrucadu/b7ad26b512d709148ae5 to your computer and use it in GitHub Desktop.
Save barrucadu/b7ad26b512d709148ae5 to your computer and use it in GitHub Desktop.
Coapplicative functors in Haskell
-- Because of the type of 'from', 'Coaplicative' must correspond to "non-empty containers", for the usual hand-wavy definition of "container".
class Functor f => Coapplicative f where
from :: f a -> a
separate :: f (Either a b) -> Either (f a) (f b)
instance Coapplicative Identity where
from (Identity a) = a
separate (Identity (Left a)) = Left (Identity a)
separate (Identity (Right b)) = Right (Identity b)
-- Silly Haskell having no nontrivial comonoids.
class Coapplicative f => Coalternative f where
full :: f a -> ()
full _ = ()
split :: f a -> f (a, a)
split = fmap (\a -> (a,a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment