Error GHC-91510: illegal polymorphic type
I had a niggle, so I came back to this.
The tweaks I introduced were an attempt to fix some errors that were actually due to the unconstrained type signatures on id and (.); they aren’t relevant to the core issue here, so technically you can leave Transform unchanged and use
class (forall x. f x) => Universally f
instance (forall x. f x) => Universally f
to declare
instance (Category cat) => Category (Transform cat) where
type Objects (Transform cat) f = Universally (ObjectClass cat . f)
However, those tweaks did point in the right direction semantically: if the true objects of the categories involved are only those satisfying the corresponding Objects constraints, then introducing an unconstrainable x as an object in the field of Transform is erroneous.
That said, imposing the constraint from cat, the target category isn’t right either; that restricts us to transformations between endofunctors. The true fix requires Transform to know about the source category:
type Transform ::
(j -> j -> Type) ->
(k -> k -> Type) ->
(j -> k) ->
(j -> k) ->
Type
newtype Transform src tgt f g = Transform
{transform :: forall x. Objects src x => tgt (f x) (g x)}
instance (Category src, Category tgt) => Category (Transform src tgt) where
type Objects (Transform src tgt) f = ObjectClass src ==> ObjectClass tgt . f
id = Transform id
Transform g_h . Transform f_g = Transform (g_h . f_g)
Discussion in the ATmosphere