I have the following program:
{-# language MultiParamTypeClasses #-}
{-# language PolyKinds #-}
{-# language QuantifiedConstraints #-}
{-# language RankNTypes #-}
{-# language ScopedTypeVariables #-}
{-# language TypeApplications #-}
{-# language UndecidableInstances #-}
module Control.IO.GWeave where
import Generics.Kind
newtype HigherOrder e m a =
HigherOrder ( e m a )
weaveFromTo
:: forall m n e a code.
( GenericK e ( LoT2 n a )
, GenericK e ( LoT2 m a )
, GWeave ( RepK e ) m n ( LoT2 m a ) ( LoT2 n a )
)
=> ( forall x. m x -> n x ) -> HigherOrder e m a -> HigherOrder e n a
weaveFromTo eta ( HigherOrder a ) =
HigherOrder ( toK ( gweave @_ @m @n @( LoT2 m a ) @( LoT2 n a ) eta ( fromK a ) ) )
class GWeave f ( m :: * -> * ) ( n :: * -> * ) as bs where
gweave :: ( forall x. m x -> n x ) -> f as -> f bs
class Effect e where
weave :: ( forall x. m x -> n x ) -> e m a -> e n a
instance
( forall n a. GenericK e ( LoT2 n a )
, forall m n a. GWeave ( RepK e ) m n ( LoT2 m a ) ( LoT2 n a )
) => Effect ( HigherOrder e ) where
weave eta =
weaveFromTo eta
In this, I have class Effect e where weave = .... I want to use DerivingVia to provide instances of Effect for any data type that is an instance of GenericK (from the kind-generics library on Hackage). This is perfectly doable, and weaveFromTo is the bulk of the work. The last step is to simply say instance Effect (HigherOrder e) where weave f = weaveFromTo f. However, GHC seems to refuse to accept this, even if I add the necessary constraints onto the instance declaration for Effect (HigherOrder e):
• Could not deduce (GWeave (RepK e) m n (LoT2 m a) (LoT2 n a))
from the context: (forall (n :: * -> *) (a :: k).
GenericK e (LoT2 n a),
forall (m :: * -> *) (n :: * -> *) (a :: k).
GWeave (RepK e) m n (LoT2 m a) (LoT2 n a))
bound by an instance declaration:
forall k (e :: (* -> *) -> k -> *).
(forall (n :: * -> *) (a :: k). GenericK e (LoT2 n a),
forall (m :: * -> *) (n :: * -> *) (a :: k).
GWeave (RepK e) m n (LoT2 m a) (LoT2 n a)) =>
Effect (HigherOrder e)
I'm unable to work out why GHC is unhappy. Can anyone see the problem? The code above should work provided you have kind-generics installed.