[Haskell-cafe] Have you seen this functor/contrafunctor combo?
Conal Elliott
conal at conal.net
Wed Jun 6 03:54:26 CEST 2012
> newtype Q p a = Q (p a -> a)
>
> instance ContraFunctor p => Functor (Q p) where
> fmap h (Q w) = Q (h . w . cmap h)
using "cmap" for contravariant map. For instance, p a = u -> a.
> instance ContraFunctor p => Applicative (Q p) where
> pure a = Q (pure a)
> Q fs <*> Q as = Q (\ r ->
> let
> f = fs (cmap ($ a) r)
> a = as (cmap (f $) r)
> in
> f a)
I've checked the functor laws but not the applicative laws, and I haven't
looked for a Monad instance.
Or extend to a more symmetric definition adding a (covariant) functor f to
the contravariant functor p.
> newtype Q' p f a = Q' (p a -> f a)
A (law-abiding) Functor instance is easy, but I don't know about an
Applicative instance.
Have you seen Q or Q' before? They look like they ought to be something
familiar & useful.
-- Conal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120605/a4d3d14e/attachment.htm>
More information about the Haskell-Cafe
mailing list