title: Is it possible to derive recursion principles generically?
body: <p>In Idris, there's some magical machinery to automatically create (dependent) eliminators for user-defined types. I'm wondering if it's possible to do something (perhaps less dependent) with Haskell types. For instance, given</p>
  
  <pre><code>data Foo a = No | Yes a | Perhaps (Foo a)
  </code></pre>
  
  <p>I want to generate</p>
  
  <pre><code>foo :: b -&gt; (a -&gt; b) -&gt; (b -&gt; b) -&gt; Foo a -&gt; b
  foo b _ _ No = b
  foo _ f _ (Yes a) = f a
  foo b f g (Perhaps c) = g (foo b f g x)
  </code></pre>
  
  <p>I'm pretty weak on polyvariadic functions and generics, so I could use a bit of help getting started.</p>