title: Haskell Typeclasses : What am I doing wrong here?
body: <p>Brand new to Haskell and trying to write a parser.</p>
  
  <p>I've used attoparsec to successfully chop my input file up into tokens into an AST.</p>
  
  <p>I now want to walk the AST and emit output from it. I thought I could do that by adding some generic routines to Token class by deriving from a type class and then providing specific functions where needed in an instance to emit code based on the Token type.</p>
  
  <p>The code is probably easier to follow than my explanation. This is what I tried:</p>
  
  <pre><code>class AST a where
    children :: a -&gt; [a]
    prefix :: a -&gt; String
    suffix :: a -&gt; String
    node :: a -&gt; [String]
  
    children v = []
    prefix v = ""
    suffix v = ""
    node v = [prefix v] ++ (concatMap node $ children v) ++ [suffix v]
  
  data Token =  Line { lnName :: String, lnLines :: Int }
              | LineList { llLines :: [Token] }
              | Init String
              | Main String
              | Step { stId :: String, stDuration :: Float }
              | Scene { scId :: String, scTokens :: [Token] }
              | Sequence { sqId :: String , sqScenes :: [Token] }
              | File {flContents :: [Token]} deriving (Show, AST)
  </code></pre>
  
  <p>So my understanding is that if I derive from the type class I've written that:</p>
  
  <ul>
  <li>I don't need to provide an instance definition as all of the functions have default implementations</li>
  <li>I can override the defaults per Token type if needed</li>
  </ul>
  
  <p>But I get an error from ghc that's not all that helpful</p>
  
  <blockquote>
    <p>Parser.hs|27 col 60 error|  Can't make a derived instance of <code>AST Token':</code>AST' is not a derivable class In the data declaration for `Token'</p>
  </blockquote>
  
  <p>Fair enough, but why is that the case? Kind of at a loss as to how to fix it without any more information. Any help gratefully received.</p>
  
  <p>I know this isn't a useful comment but I have to say, absolutely loving Haskell. It's been a joy to learn :)</p>