During play around objective package, I noticed following type has interesting property.
> {-# LANGUAGE RankNTypes #-}
> data N f r = N { unN :: forall x. f x -> (x, r) }
It is a Functor.
> instance Functor (N f) where
> fmap f (N nat) = N $ fmap (fmap f) nat
> -- or, = N $ \fx -> let { (x,a) = nat fx } in (x, f a)
After few hours of google/hoogle, I gave up finding any existing module that includes this type. What is this type? If it is well known, what is the name? Is this useful or ignored because useless?
This is not my 100% original creation, because N was derived from Object found in objective package.
> data Object f g = Object {
> runObject :: forall x. f x -> g (x, Object f g)
> }
N f
is a Functor which yields Object f Identity
when Fix is applied to.
Following is a fact about this type and why I thought it is interesting.
N converts Reader to Writer, vice versa. (Here I used (=) symbol for isomorphism between types)
N ((->) e) r
= forall x. (e -> x) -> (x, r)
= (e, r)
N ((,) d) r
= forall x. (d, x) -> (x, r)
= d -> r
N converts Store comonad to State monad, but inverse is not true.
> data Store s a = Store s (s -> a)
> type State s a = s -> (s, a)
N (Store s) r
= forall x. (s, (s -> x)) -> (x, r)
= forall x. s -> (s -> x) -> (x, r)
= s -> (s, r)
= State s r
N (State s) r
= forall x. (s -> (s, x)) -> (x, r)
= forall x. (s -> s, s -> x) -> (x, r)
= forall x. (s -> s) -> (s -> x) -> (x, r)
= (s -> s) -> (s, r) -- ???
N can't take Maybe.
N Maybe r
= forall x. Maybe x -> (x, r)
= forall x. (() -> (x, r), x -> (x, r))
= Void -- because (() -> (x, r)) can't be implemented
Following function may be fun. I couldn't do it's inverse.
> data Cofree f a = Cofree a (f (Cofree f a))
> data Free f a = Pure a | Wrap (f (Free f a))
> unfree :: Free (N f) r -> N (Cofree f) r
> unfree (Pure r) = N $ \(Cofree a _) -> (a, r)
> unfree (Wrap n_f) = N $
> \(Cofree _ f) -> let (cofree', free') = unN n_f f
> in unN (unfree free') cofree'
Entire post is literate Haskell (.lhs).
I call it a "handler" functor. Object
used to be defined using the handler functor before I released objective.
Yeah, this functor is interesting -- Cofree (Handler f)
has a public getter and Free (Handler f)
is a mortal object. Maybe I should have shipped the handler functor...