Skip to content

Commit

Permalink
coapplicatives are the monoids of contravariant Day convolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Aug 2, 2014
1 parent f45405e commit 65d9dd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Data/Functor/Contravariant/Coapplicative.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Data.Functor.Contravariant.Coapplicative where

import Data.Functor.Contravariant
import Data.Monoid

class Contravariant f => Coapplicative f where
divide :: (a -> (b, c)) -> f b -> f c -> f a
conquer :: f a

instance Monoid r => Coapplicative (Op r) where
divide f (Op g) (Op h) = Op $ \a -> case f a of
(b, c) -> g b `mappend` h c
conquer = Op $ const mempty

instance Coapplicative Comparison where
divide f (Comparison g) (Comparison h) = Comparison $ \a b -> case f a of
(a',a'') -> case f b of
(b',b'') -> g a' b' `mappend` h a'' b''
conquer = Comparison $ \_ _ -> EQ

instance Coapplicative Equivalence where
divide f (Equivalence g) (Equivalence h) = Equivalence $ \a b -> case f a of
(a',a'') -> case f b of
(b',b'') -> g a' b' && h a'' b''
conquer = Equivalence $ \_ _ -> True

instance Coapplicative Predicate where
divide f (Predicate g) (Predicate h) = Predicate $ \a -> case f a of
(b, c) -> g b && h c
conquer = Predicate $ const True
1 change: 1 addition & 0 deletions contravariant.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library

exposed-modules:
Data.Functor.Contravariant
Data.Functor.Contravariant.Coapplicative
Data.Functor.Contravariant.Compose

ghc-options: -Wall

0 comments on commit 65d9dd7

Please sign in to comment.