Search code examples
haskellambiguous-type-variable

Ambiguous type variable ‘a0’ arising from


I am a relatively new Haskell user. I'm getting an error Ambiguous type variable ‘a0’ arising from (variable name) prevents the constraint ‘(Field a0)’ from being solved. from GHCi for each variable in my code arising in the call of tau. Here are all the relevant declarations:

import Linear as L
import Data.Vector (Vector)
import Numeric.LinearAlgebra.HMatrix as HM
import Data.List

find_tau d v0 v =
    let m' = (d><d) (concat (map (^-^ v0) v)) in
    toLists (inv (tr' m'))

d = 2
pk = 16.0
eps = 0.05
s2 = sqrt 2.0
ga = s2/2.0
v0' = [0.5*(1.0 + ga*ga + ga*eps)*pk, 0.5*(-1.0 + ga*ga + ga*eps)*pk*s2/2.0]
v1' = [0.5*(1.0 + ga*ga - ga*eps)*pk, 0.5*(-1.0 + ga*ga - ga*eps)*pk*s2/2.0]
v2' = [0.5*(-1.0 + ga*ga - ga*eps)*pk, 0.5*(1.0 + ga*ga - ga*eps)*pk*s2/2.0]
v' = [v1',v2']
tau = find_tau d v0' v'

And here is an example error:

Debug.hs:17:8: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘*’
      prevents the constraint ‘(Num a0)’ from being solved.
      Relevant bindings include v2' :: [a0] (bound at Debug.hs:17:1)
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance RealFloat a => Num (Complex a)
          -- Defined in ‘Data.Complex’
        instance (Container Matrix a, Num a, Num (HM.Vector a)) =>
                 Num (Matrix a)
          -- Defined in ‘hmatrix-0.20.0.0:Numeric.Matrix’
        instance (Integral t, GHC.TypeNats.KnownNat n) => Num (Mod n t)
          -- Defined in ‘hmatrix-0.20.0.0:Internal.Modular’
        ...plus 15 others
        ...plus 48 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: 0.5 * (- 1.0 + ga * ga - ga * eps) * pk
      In the expression:
        [0.5 * (- 1.0 + ga * ga - ga * eps) * pk,
         0.5 * (1.0 + ga * ga - ga * eps) * pk * s2 / 2.0]
      In an equation for ‘v2'’:
          v2'
            = [0.5 * (- 1.0 + ga * ga - ga * eps) * pk,
               0.5 * (1.0 + ga * ga - ga * eps) * pk * s2 / 2.0]
   |
17 | v2' = [0.5*(-1.0 + ga*ga - ga*eps)*pk, 0.5*(1.0 + ga*ga - ga*eps)*pk*s2/2.0]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What is the standard way to resolve this? My grasp of Haskell's inner workings is poor, so any help is greatly appreciated.


Solution

  • The solution to ambiguity is to be specific - specify what the types are that the compiler tells you could be one of many types:

    d :: Int
    pk, eps, s2, ga :: Double