Generically derive Arbitrary for massive algebraic data types? : <p>I've got a protocol that I've typed like so:</p>

<pre><code>data ProtocolPacket
  = Packet1 Word8 Text Int8
  | Packet2 Text
  | Packet3 Int Text Text Text Text
  | Packet4 Int Double Double Double Int16 Int16 Int16
  ...
  deriving (Show,Eq)
</code></pre>

<p>In addition, I've implemented serialization/deserialization code for each packet. Naturally, I would like to test this protocol in Quickcheck and make sure that serializing and deserializing any packet for any combination of inputs will give me back exactly what I put in. So I go ahead and implement these packets for the <code>Arbitrary</code> type class like so:</p>

<pre><code>instance Arbitrary ProtocolPacket where
  arbitrary = do
  packetID &lt;- choose (0x00,...) :: Gen Word8
  case packetID of
    0x00 -&gt; do
      a &lt;- arbitrary
      b &lt;- arbitrary
      c &lt;- arbitrary
      return $ Packet1 a b c
    0x01 -&gt; do
      a &lt;- arbitrary
      return $ Packet2 a
    0x02 -&gt; do
      a &lt;- arbitrary
      b &lt;- arbitrary
      c &lt;- arbitrary
      d &lt;- arbitrary
      e &lt;- arbitrary
      return $ Packet3 a b c d e
    0x03 -&gt; do
      a &lt;- arbitrary
      b &lt;- arbitrary
      c &lt;- arbitrary
      d &lt;- arbitrary
      e &lt;- arbitrary
      f &lt;- arbitrary
      g &lt;- arbitrary
      return $ Packet4 a b c d e f g
    ...
</code></pre>

<p>Assume that I've gone ahead and defined <code>Arbitrary</code> for all relevant data constructor arguments that don't have <code>Arbitrary</code> defined out of the box, such code needs to be hand-written by me in order for the packet fields to be populated with meaningful data. But that's it.</p>

<p>But as you can see, I'm repeating myself <em>a lot</em> for something that is just grunt work. And this is a small sample of what I'm actually dealing with. Ideally, I would like to be able to just do this:</p>

<pre><code>{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics

data ProtocolPacket
  = Packet1 Word8 Text Int8
  | Packet2 Text
  | Packet3 Int Text Text Text Text
  | Packet4 Int Double Double Double Int16 Int16 Int16
  ...
  deriving (Show,Eq,Generic)

instance Arbitrary ProtocolPacket
</code></pre>

<p>like I can do with <code>FromJSON</code> and <code>ToJSON</code>, but this doesn't work. Is there there a method that does?</p>
