title: Haskell: Non type-variable argument in the constraint?
body: <p>This is the code, it is a bit non-straightforward I apologize. Basically the function vecF is supposed to have two results v1,v2 if entering two inputs v0 and vs, but for some reason I end up getting error messages.</p>
  
  <pre><code>newtype Vector2 a = Vector2 (a,a)
    deriving (Show,Eq)
  newtype Vector3 a = Vector3 (a,a,a)
    deriving (Show,Eq)
  newtype Vector4 a = Vector4 (a,a,a,a)
    deriving (Show,Eq)
  
  class VectorSpace v where
    vecZero       :: (Num a) =&gt; v a
    vecSum        :: (Num a) =&gt; v a -&gt; v a -&gt; v a
    vecScalarProd :: (Num a) =&gt; a -&gt; v a -&gt; v a
    vecMagnitude  :: (Floating a) =&gt; v a -&gt; a
    vecInnerProd  :: (Num a) =&gt; v a -&gt; v a-&gt; a
  
  instance VectorSpace Vector2 where
    vecZero = Vector2 (0,0)
    vecSum (Vector2 (q,w)) (Vector2 (a,s)) = Vector2 (q+a,w+s)
    vecScalarProd x (Vector2 (q,w)) = Vector2 (q*x,w*x)
    vecMagnitude (Vector2 (q,w)) = sqrt(q*q+w*w)
    vecInnerProd (Vector2 (q,w)) (Vector2 (a,s)) = q*a+w*s
  
  instance VectorSpace Vector3 where
    vecZero = Vector3 (0,0,0)
    vecSum (Vector3 (q,w,e)) (Vector3 (a,s,d)) = Vector3 (q+a,w+s,e+d)
    vecScalarProd x (Vector3 (q,w,e)) = Vector3 (q*x,w*x,e*x)
    vecMagnitude (Vector3 (q,w,e)) = sqrt(q*q+w*w+e*e)
    vecInnerProd (Vector3 (q,w,e)) (Vector3 (a,s,d)) = q*a+w*s+e*d
  
  instance VectorSpace Vector4 where
    vecZero = Vector4 (0,0,0,0)
    vecSum (Vector4 (q,w,e,r)) (Vector4 (a,s,d,f)) = Vector4 
  (q+a,w+s,e+d,r+f)
    vecScalarProd x (Vector4 (q,w,e,r)) = Vector4 (q*x,w*x,e*x,r*x)
    vecMagnitude (Vector4 (q,w,e,r)) = sqrt(q*q+w*w+e*e+r*r)
    vecInnerProd (Vector4 (q,w,e,r))(Vector4 (a,s,d,f)) =
      q*a+w*s+e*d+r*f
  
  vecF :: (Floating a, Ord a, VectorSpace v) =&gt; v a -&gt; [v a] -&gt; (v a, v a)
  vecF v0 vs = (v1,v2) where
              v0Neg     = vecScalarProd (-1) v0
              v1MinMag  = minimumBy (comparing snd)(zip [0..]  
  [vecMagnitude (vecSum v0Neg v) | v &lt;- vs])
              v1Index   = fst v1MinMag
              v1Min     = [vecSum v0Neg v | v &lt;- vs] !! v1Index
              v1        = vecSum v0 v1Min
  
              v2MaxMag  = maximumBy (comparing snd)(zip [0..]  
  [vecMagnitude (vecSum v0Neg v) | v &lt;- vs])
              v2Index   = fst v2MaxMag
              v2Max     = [vecSum v0Neg v | v &lt;- vs] !! v2Index
              v2        = vecSum v0 v2Max
  </code></pre>
  
  <p>Below is errors I got when i enter:</p>
  
  <pre><code> vecF (1,2,3,3) [(2,1,2,2),(13,3,2,1)]
  </code></pre>
  
  <p>.</p>
  
  <pre><code> •Non type-variable argument
      in the constraint: VectorSpace ((,,,) t2 t1 t)
    (Use FlexibleContexts to permit this)
    •When checking the inferred type
      it :: forall a t t1 t2.
            (Num t, Num t1, Num t2, VectorSpace ((,,,) t2 t1 t), Ord a,
             Floating a) =&gt;
            ((t2, t1, t, a), (t2, t1, t, a))
  </code></pre>
  
  <p>I have tried using FlexibleContexts like this, </p>
  
  <pre><code>:set -XFlexibleContexts
  </code></pre>
  
  <p>and then I get this new error message</p>
  
  <pre><code> • When checking the inferred type
      it :: forall a t t1 t2.
            (Num t, Num t1, Num t2, VectorSpace ((,,,) t2 t1 t), Ord a,
             Floating a) =&gt;
            ((t2, t1, t, a), (t2, t1, t, a))
  • No instance for (VectorSpace ((,,,) t2 t1 t0))
      arising from a use of ‘it’
  • In a stmt of an interactive GHCi command: print it
  </code></pre>
  
  <p>Is there any way I can fix this?</p>