title: How to write a Serialize instance for this GADT?
body: <p>Below is a module that attempts to implement a <code>Serialize</code> instance for a simple GADT.  Unfortunately the <code>get</code> implementation for the <code>Reorder</code> constructor complains that there is no <code>Ixed a</code> constraint.  Is there any way, beautiful or ugly, to implement this?  I can't add <code>Ixed a</code> to the instance context because the <code>Update</code> constructor needs to work for values that don't satisfy this constraint.</p>
  <pre><code>{-# LANGUAGE GADTs #-}
  
  import Control.Lens (Index, Ixed)
  import Data.Serialize
  
  -- | Two different ways of updating a value - replacing it completely or,
  -- if it is an instance of Ixed, re-ordering it.
  data Op a where
    Update :: Serialize a =&gt; a -&gt; Op a
    Reorder :: (Ixed a, Serialize (Index a)) =&gt; [Index a] -&gt; Op a
  
  instance Serialize a =&gt; Serialize (Op a) where
    put (Update a) = putWord8 1 &gt;&gt; put a
    put (Reorder ks) = putWord8 2 &gt;&gt; put ks
    get = do
      i &lt;- getWord8
      case i of
        1 -&gt; Update &lt;$&gt; get
        2 -&gt; Reorder &lt;$&gt; get
        _ -&gt; error &quot;instance Serialize (Op a) - corrupt data&quot;
  </code></pre>
  <hr />
  <p>Addendum: One simplification to this might be to make the type variable <code>a</code> a phantom type, so that <code>Op</code> looks like this:</p>
  <pre><code>data Op a where
    Update :: Serialize a =&gt; ByteString -&gt; Op a
    Reorder :: (Ixed a, Serialize (Index a)) =&gt; [ByteString] -&gt; Op a
  </code></pre>
  <p>The type could then be used to properly decode the byte strings. Not sure whether this helps</p>