I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE QuantifiedConstraints, StandaloneDeriving, UndecidableInstances #-}
module HKD2 where
import Control.Monad.Identity
type family HKD f a where
HKD Identity a = a
HKD f a = f a
data Result f = MkResult
{ foo :: HKD f Int
, bar :: HKD f Bool
}
deriving instance (forall a. Show a => Show (HKD f a)) => Show (Result f)
This results in the infuriatingly self-contradicting error message:
Could not deduce
Show (HKD f a)from the context:forall a. Show a => Show (HKD f a)
Is there a way to do this without being long-winded about it and doing
deriving instance (Show (HKD f Int), Show (HKD f Bool)) => Show (Result f)
?
I don't think you can do such thing, but I could certainly be wrong. In your example you are missing an extra constraint
Show (f a)in order for it to be complete:But that would mean that
Showinstance forf acannot depend ona, which can be true for specificf, but not in general.Edit
But at the same time it is possible to write something like that without the
TypeFamilies:So, I am not sure why GHC can't figure it out.
Edit 2
Here is an interesting observation, this compiles:
and works as expected:
So, it looks like matching on
fsomehow messes up the type checker.Worth noting, that restricting to
Show (HDK f a)even for the simplified example also gives the same compile time error as in the question: