title: Why does a sum-type of `a` containing a list constructor not match a list of `a`?
body: <p>I am trying to implement <a href="https://en.wikipedia.org/wiki/Primitive_recursive_function" rel="nofollow noreferrer">primitive recursive functions</a>. As apparently it is not common to use variadic functions in Haskell I use a type</p>
  
  <pre><code>data PrimT a = Sgl a | Sqc [a]
  </code></pre>
  
  <p>so I can pass atomic values and lists of values to functions. This works alright for the zero function, projection, and the successor function:</p>
  
  <pre><code>zero k (Sqc args) = if length args == k then Right 0 else Left "invalid number of arguments"
  zero 1 (Sgl arg)  = Right 0
  
  pi i k (Sqc args) = if length args == k then Right $ args!!i else Left "invalid number of arguments"
  pi 1 1 (Sgl arg) = Right arg
  
  nu (Sgl i) = Sgl $ i + 1
  </code></pre>
  
  <p>But I am getting problems with composition (here <code>o</code>). The idea behind this definition is that composition can happen with a function f and: 1) a list of functions and a list of arguments, 2) a list of functions and one argument, 3) one function and a list of arguments and finally 4) one function and one argument. Therefore I put the functions and in a <code>PrimT</code> so I can have cases for <code>Sqc gs</code> for a list of functions and <code>Sgl g</code> for a single function. Same for the arguments: <code>Sqc args</code> and <code>Sgl arg</code>.</p>
  
  <pre><code>o :: PrimT (PrimT b -&gt; PrimT c) -&gt; PrimT (PrimT a -&gt; PrimT b) -&gt; PrimT a -&gt; PrimT c
  o (Sgl f) (Sqc gs) (Sqc args) = f [g args | g &lt;- gs]
  -- o (Sgl f) (Sqc gs) (Sgl arg) = f [g arg | g &lt;- gs]
  -- o (Sgl f) (Sgl g) (Sqc args) = f [g args]
  -- o (Sgl f) (Sgl g) (Sgl arg) = f [g arg]
  </code></pre>
  
  <p>However the compiler is not happy with the part <code>f [g args | g &lt;- gs]</code>. It says:</p>
  
  <pre><code>primrec.hs:69:35: error:
      • Couldn't match expected type ‘PrimT b’
                    with actual type ‘[PrimT b]’
      • In the first argument of ‘f’, namely ‘[g args | g &lt;- gs]’
        In the expression: f [g args | g &lt;- gs]
        In an equation for ‘o’:
            o (Sgl f) (Sqc gs) (Sqc args) = f [g args | g &lt;- gs]
      • Relevant bindings include
          gs :: [PrimT a -&gt; PrimT b] (bound at primrec.hs:69:16)
          f :: PrimT b -&gt; PrimT c (bound at primrec.hs:69:8)
          o :: PrimT (PrimT b -&gt; PrimT c)
               -&gt; PrimT (PrimT a -&gt; PrimT b) -&gt; PrimT a -&gt; PrimT c
            (bound at primrec.hs:69:1)
     |
  69 | o (Sgl f) (Sqc gs) (Sqc args) = f [g args | g &lt;- gs]
     |                                   ^^^^^^^^^^^^^^^^^^
  
  primrec.hs:69:38: error:
      • Couldn't match expected type ‘PrimT a’ with actual type ‘[a]’
      • In the first argument of ‘g’, namely ‘args’
        In the expression: g args
        In the first argument of ‘f’, namely ‘[g args | g &lt;- gs]’
      • Relevant bindings include
          g :: PrimT a -&gt; PrimT b (bound at primrec.hs:69:45)
          args :: [a] (bound at primrec.hs:69:25)
          gs :: [PrimT a -&gt; PrimT b] (bound at primrec.hs:69:16)
          o :: PrimT (PrimT b -&gt; PrimT c)
               -&gt; PrimT (PrimT a -&gt; PrimT b) -&gt; PrimT a -&gt; PrimT c
            (bound at primrec.hs:69:1)
     |
  69 | o (Sgl f) (Sqc gs) (Sqc args) = f [g args | g &lt;- gs]
     |
  </code></pre>
  
  <p>But I don't understand why. <code>PrimT a</code> can be a list of <code>a</code> (<code>[a]</code>) according to its definition. So where is the problem?</p>