ekmett / contravariant

Haskell 98 contravariant functors
http://hackage.haskell.org/package/contravariant
Other
73 stars 24 forks source link

[RFC] Add monadic versions of data types and abstractions? #62

Open chshersh opened 5 years ago

chshersh commented 5 years ago

I've discovered that if contravariant data types have access to a monad, it's possible to implement monadic versions of them:

newtype Predicate m a = Predicate
    { getPredicate :: a -> m Bool
    }

newtype Comparison m a = Comparison
    { getComparison :: a -> a -> m Ordering
    }

newtype Op m a b = Op
    { getOp :: b -> m a
    }

class ContravariantM t where
    cmapM :: Monad m => (a -> m b) -> t m b -> t m a

class DivisibleM t where
    divide :: Monad m => (a -> m (b, c)) -> t m b -> t m c -> t m a

class DecidableM t where
    decide :: Monad m => (a -> m (Either b c)) -> t m b -> t m c -> t m a

Monadic contravariant functions like the above ones are used in co-log. Does it make sense in general to have such abstractions? And do they have a proper name?