Description
We use coerceC
to model Core's casts. However, it introduces some problems. For one, it basically duplicates the functionality of RepCat
, but for different types (Coercible
ones). The other problem is that it means all newtypes that are ever coerced need to have their constructors in scope whenever categorize
is called. This is especially problematic for private constructors.
Conal has brought up this issue in both
- Improved handling of GHC Core coercions compiling-to-categories/concat#10 and
- Simple, dependable recipe for client modules compiling-to-categories/concat#34.
If we had something along the lines of
instance {-# overlappable #-} Newtype a b => HasRep a where
type Rep a = b
abst = Newtype.pack
repr = Newtype.unpack
could we avoid the newtype constructor scoping issues? (At the expense of using DeriveAnyClass
and deriving (Newtype)
on all newtypes) If we manage to switch to Generics, this could be even simpler.
The complicated part being that Coercions get combined, so a coercion like TyConAppCo Representational sumTyCon [TyConAppCo Representational productTyCo []]
would become abstC . abstC
, not just a single abstC
. I think we can approach this by inspecting the coercion and converting it into a composition of HasRep
operations. Could be complicated to implement, though, because I'm not sure how to destructure Coercions, since they're all private.
(Extricated from https://kitty-hawk.atlassian.net/browse/SW-2657)