title: Generic method that expands its return type
body: <p>I have the data type for matrices defined as <code>Matrix a</code>. In most places in my program, I use it with either strings as <code>Matrix String</code> or with other matrices as <code>Matrix (Matrix a)</code>.</p>
  
  <p>I need to write a function that pads the content of the matrix according to certain rules. For string matrices the signature would be:</p>
  
  <pre><code>pad :: Matrix String -&gt; Matrix (Matrix String)
  </code></pre>
  
  <p>because each string is converted into a matrix of a certain size.</p>
  
  <p>For matrices with other matrices in them, the function should be:</p>
  
  <pre><code>pad :: Matrix (Matrix a) -&gt; Matrix (Matrix a)
  </code></pre>
  
  <p>because in this case only the size of the inner matrices is changing.</p>
  
  <p>Is there a way to generalize those two? I tried this:</p>
  
  <pre><code>class Paddable b =&gt; Paddable a where
      pad :: a -&gt; Matrix b
  </code></pre>
  
  <p>and then declaring <code>String</code> and <code>Matrix a</code> as <code>Paddable</code>, but this fails with an obvious "Could not deduce... from the context" error, which is expected as I don't define my <code>pad</code> function to cover all possible values of <code>b</code>.</p>
  
  <p>Any suggestions here?</p>