`coerce`ing a function should be possible without type applications when we can newtype derive the method
Today, I've been bitten again by the fact that I can write
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE RoleAnnotations #-}
module Lib where
import Data.Coerce
class C a where
emp :: a
instance C [a] where
emp = []
newtype MyList a = MyList [a]
deriving C
type role MyList representational -- just to be sure this isn't an issue
But I can't write
empty :: [a]
empty = []
myEmpty :: MyList a
myEmpty = coerce empty
It works if I insert the necessary -XTypeApplications. But that is so ugly! I really want it to be inferred for me, just as the deriving strategy does (and hence GHC already knows what to do):
==================== Derived instances ====================
Derived class instances:
instance Lib.C (Lib.MyList a) where
Lib.emp :: Lib.MyList a_ago
Lib.emp
= GHC.Prim.coerce
@([a_ago]) @(Lib.MyList a_ago) (Lib.emp @([a_ago]))
This came up while I wanted to have
newtype Prio = Prio Int
newtype PrioMap a = PrioMap (IntMap a)
insertPrioMap :: Prio -> a -> PrioMap a -> PrioMap a
insertPrioMap = coerce IntMap.insert
Which also doesn't work without further elaboration.