Make coerce-derived dictionaries coercible
Take the Alt type class as an example, which is Alternative without the empty method:
{-# Language ConstrainedClassMethods #-}
{-# Language DerivingVia #-}
import Control.Applicative
class Functor f => Alt f where
(<!>) :: f a -> f a -> f a
some :: Applicative f => f a -> f [a]
some v = some_v
where many_v = some_v <!> pure []
some_v = (:) <$> v <*> many_v
many :: Applicative f => f a -> f [a]
many v = many_v
where many_v = some_v <!> pure []
some_v = (:) <$> v <*> many_v
The applicative constraints on the methods means we cannot derive Alt.
If we try to derive it (say via []) GHC fails to coerce between Applicative [] and Applicative List.
instance Alt [] where
(<!>) = (<>)
newtype List a = List [a]
-- • Couldn't match type ‘[]’ with ‘List’
-- arising from the coercion of the method ‘many’
-- from type ‘forall a. Applicative [] => [a] -> [[a]]’
-- to type ‘forall a. Applicative List => List a -> List [a]’
-- • When deriving the instance for (Alt List)
--
-- .. same for ‘some’
deriving (Functor, Applicative, Alt)
via []
GHC can make use of the provenence of the Applicative instance: we know that that Applicative List was derived via Applicative [] using coerce-based deriving (either newtype, via) and so the dictionaries can be considered Coercible (?). If not I'm curious if this fails.¹
This is not the first time I want this (I forgot in what context though), let me know if it's sensible/useful.
¹ Instead of coercing method-by-method could coerce-based deriving coerce the whole dictionary?