A Formula for Codensity Monads and Density Comonads

For a functor F whose codomain is a cocomplete, cowellpowered category $$\mathcal {K}$$K with a generator S we prove that a codensity monad exists iff for every object s in S all natural transformations from $$\mathcal {K}(X, F-)$$K(X,F-) to $$\mathcal {K}(s, F-)$$K(s,F-) form a set. Moreover, the codensity monad has an explicit description using the above natural transformations. Concrete examples are presented, e.g., the codensity monad of the power-set functor $$\mathcal {P}$$P assigns to every set X the set of all nonexpanding endofunctions of $$\mathcal {P}X$$PX. Dually, a set-valued functor F is proved to have a density comonad iff all natural transformations from $$X^F$$XF to $$2^F$$2F form a set. Moreover, that comonad assigns to X the set of all those transformations. For preimages-preserving endofunctors F of $${\mathsf {Set}}$$Set we prove that F has a density comonad iff F is accessible.