-->

What is the general case of QuickCheck's promo

2020-03-13 05:55发布

问题:

What is the general term for a functor with a structure resembling QuickCheck's promote function, i.e., a function of the form:

promote :: (a -> f b) -> f (a -> b)

(this is the inverse of flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b)). Are there even any functors with such an operation, other than (->) r and Id? (I'm sure there must be). Googling 'quickcheck promote' only turned up the QuickCheck documentation, which doesn't give promote in any more general context AFAICS; searching SO for 'quickcheck promote' produces no results.

回答1:

So far I found these ways of constructing an f with the promote morphism:

  • f = Identity
  • if f and g both have promote then the pair functor h t = (f t, g t) also does
  • if f and g both have promote then the composition h t = f (g t) also does
  • if f has the promote property and g is any contrafunctor then the functor h t = g t -> f t has the promote property

The last property can be generalized to profunctors g, but then f will be merely a profunctor, so it's probably not very useful, unless you only require profunctors.

Now, using these four constructions, we can find many examples of functors f for which promote exists:

f t = (t,t)

f t = (t, b -> t)

f t = (t -> a) -> t

f t = ((t,t) -> b) -> (t,t,t)

f t = ((t, t, c -> t, (t -> b) -> t) -> a) -> t

Also note that the promote property implies that f is pointed.

point :: t -> f t
point x = fmap (const x) (promote id)

Essentially the same question: Is this property of a functor stronger than a monad?



回答2:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b
(=<<) :: Monad m => (a -> m b) -> m a -> m b

Given that Monad is more powerful an interface than Applicative, this tell us that a -> f b can do more things than f (a -> b). This tells us that a function of type (a -> f b) -> f (a -> b) can't be injective. The domain is bigger than the codomain, in a handwavey manner. This means there's no way you can possibly preserve behavior of the function. It just doesn't work out across generic functors.

You can, of course, characterize functors in which that operation is injective. Identity and (->) a are certainly examples. I'm willing to bet there are more examples, but nothing jumps out at me immediately.



回答3:

Data.Distributive has

class Functor g => Distributive g where
  distribute :: Functor f => f (g a) -> g (f a)
  -- other non-critical methods

Renaming your variables, you get

promote :: (c -> g a) -> g (c -> a)

Using slightly invalid syntax for clarity,

promote :: ((c ->) (g a)) -> g ((c ->) a)

(c ->) is a Functor, so the type of promote is a special case of the type of distribute. Thus every Distributive functor supports your promote. I don't know if any support promote but not Distributive.