What to do with "Inferred type less polymorphic than expected"?

I need the Numeric.FAD library, although it is still completely perplexed by existential types.

This is the code:

error_diffs :: [Double] -> NetworkState [(Int, Int, Double)] error_diffs desired_outputs = do diff_error <- (diff_op $ error' $ map FAD.lift desired_outputs)::(NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double)) weights <- link_weights let diffs = FAD.grad (diff_error::([FAD.Dual tag a] -> FAD.Dual tag b)) weights links <- link_list return $ zipWith (\link diff -> (linkFrom link, linkTo link, diff) ) links diffs 

error "is executed in the reader monad running diff_op, which in turn generates an anonymous function to take the current NetworkState and differential inputs from FAD.grad and paste them into Reader.

Haskell confuses me:

 Inferred type is less polymorphic than expected Quantified type variable `tag' is mentioned in the environment: diff_error :: [FAD.Dual tag Double] -> FAD.Dual tag Double (bound at Operations.hs:100:33) In the first argument of `FAD.grad', namely `(diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b)' In the expression: FAD.grad (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights In the definition of `diffs': diffs = FAD.grad (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights 
+4
source share
2 answers

If I write

 bigNumber :: (Num a) => a bigNumber = product [1..100] 

then when bigNumber :: Int is evaluated,
it evaluates (product :: [Int] -> Int) [(1 :: Int) .. (100 :: Int)] ,

and when bigNumber :: Integer is evaluated,
it evaluates (product :: [Integer] -> Integer) [(1 :: Integer) .. (100 :: Integer)] .

Nothing is shared between them.

error_diffs has one type, that is: [Double] -> NetworkState [(Int, Int, Double)] . It must be evaluated in one way.

However, what you have inside:

 ... :: NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double) 

can be evaluated differently depending on what tag .

See the problem?

+3
source

this code gives the same error as you:

 test :: Int test = (res :: Num a => a) where res = 5 

The compiler realized that res always of type Int and is worried that for some reason you think res is polymorphic.

this code, however, works just fine:

 test :: Int test = res where res :: Num a => a res = 5 

here, res defined as polymorphic, but only ever used as Int . the compiler only bothers when you enter nested expressions this way. in this case, res could be reused, and perhaps one of these applications would not use it as Int , unlike when you enter a nested expression that cannot be reused by itself.

+4
source

Source: https://habr.com/ru/post/1286104/


All Articles