title: GADT Type as Shapeless Coproduct -- how to build an Interpreter with an arbitrary number of Algebras
body: <p>Let say I have two GADT types.</p>
  
  <pre><code>  abstract class Numbers[A]()
    case class IntType() extends Numbers[Int]
  
    abstract class Letters[A]()
    case class EnglishType() extends Letters[String]
  </code></pre>
  
  <p>And I have an Interpreter for each of the GADT types which will print out a description for each of the GADT subtypes.</p>
  
  <pre><code>  trait Interpreter[ALG[_],A] {
      def description(a: ALG[A]) : String
    }
  
    case class NumbersInterpreter[A]() extends Interpreter[Numbers,A] {
      override def description(a: Numbers[A]): String =
        a match {
          case i: IntType =&gt; "Int"
        }
    }
  
    case class LettersInterpreter[A]() extends Interpreter[Letters,A] {
      override def description(a: Letters[A]): String =
        a match {
          case e: EnglishType =&gt; "English"
        }
    }
  </code></pre>
  
  <p>I'm looking to combine the two GADTs into a single GADT called All</p>
  
  <pre><code>  type All[A] = Numbers[A] :+: Letters[A] :+: CNil
  </code></pre>
  
  <p>I can create a new interpreter by hard-coding all the GADT values.</p>
  
  <pre><code>  case class DualInterpreter[A](
      numbersInterpreter: NumbersInterpreter[A],
      lettersInterpreter: LettersInterpreter[A]) extends Interpreter[All,A] {
      override def description(a: All[A]): String =
        a match {
          case Inl(num) =&gt; numbersInterpreter.description(num)
          case Inr(Inl(let)) =&gt; lettersInterpreter.description(let)
          case _ =&gt; sys.error("Unreachable Code")
        }
    }
  </code></pre>
  
  <p>However, I would like to add a bunch of GADT algebras and interpreters and arbitrarily combine them into a single algebra so I'm looking for a more generic approach to replace the <code>DualInterpreter</code> above.  I can see the type signature is something along the lines of</p>
  
  <pre><code>  case class ArbitraryInterpreter[ALG[_]&lt;:Coproduct,A](???) extends Interpreter[ALG,A] {
      override def description(a: ALG[A]): String = ???
    }
  </code></pre>
  
  <p>The main thing I would like to abstract away is the pattern matching inside of the <code>description</code> method because it could get pretty ugly with the number of available algebras.  It would be have an interpreter where the constructor arguments are the interpreters and the pattern matching delegates to the appropriate interpreter based on the type ALG passed into the description method. </p>