title: Can I generalize over class constraints in Haskell?
body: <p>In my application I have a type definition that looks like this:</p>
  
  <pre><code>{-# LANGUAGE ExistentialQuantification #-}
  
  class C a where
  
  data A = forall a. C a =&gt; A { unA :: a }
  </code></pre>
  
  <p>I most definitely want the kind signature <code>A :: Type</code>.</p>
  
  <p>I would like to generalize over the class constraint, like so:</p>
  
  <pre><code>{-# LANGUAGE ExistentialQuantification #-}
  {-# LANGUAGE GADTs                     #-}
  {-# LANGUAGE KindSignatures            #-}
  {-# LANGUAGE ScopedTypeVariables       #-}
  
  import Data.Kind (Type, Constraint)
  
  class C a where
  
  data A' (c :: Type -&gt; Constraint) where
    A' :: forall a. c a =&gt; { unA :: a } -&gt; A' c
  
  type A = A' C
  </code></pre>
  
  <p>Note that <code>A</code> should be isomorphic to the <code>A</code> as defined in the beginning.</p>
  
  <p>However GHC (8.6.5) rejects the generalized definition:</p>
  
  <pre><code>
  generalize.hs:11:19: error: Not in scope: type variable ‘c’
     |
  11 |   A' :: forall a. c a =&gt; { unA :: a } -&gt; A' c
     |                   ^
  
  generalize.hs:11:45: error: Not in scope: type variable ‘c’
     |
  11 |   A' :: forall a. c a =&gt; { unA :: a } -&gt; A' c
     |                                             ^
  </code></pre>
  
  <p>I don't understand the error, since I did enable <code>ScopedTypeVariables</code>.</p>
  
  <p>Am I missing something obvious, or is what I'm trying to do not possible? If so, why not?</p>