Type error when testing a function with a negative number

☆樱花仙子☆ 提交于 2019-12-18 09:16:11

问题


I am following along with the Learn you a Haskell for great good, I have implemented take':

take' :: (Ord i, Num i) => i -> [a] -> [a]
take' n _
  | n <= 0 = []
take' _ [] = []
take' n (x:xs) = x: take' (n-1) xs

When testing the function with:

take' -2 [2]

instead of getting an empty list, I have this message:

Non type-variable argument in the constraint: Num (i -> [a] -> [a])
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall i a t.
            (Num i, Num t, Num (i -> [a] -> [a]), Num ([t] -> i -> [a] -> [a]),
             Ord i) =>
            i -> [a] -> [a]

I have added a space between - and 2 as suggested, and it leads to the same error:

*Main> take' - 2 [2]

<interactive>:78:1:
    Non type-variable argument in the constraint: Num (i -> [a] -> [a])
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall i a t.
            (Num i, Num t, Num (i -> [a] -> [a]), Num ([t] -> i -> [a] -> [a]),
             Ord i) =>
            i -> [a] -> [a]

回答1:


As already discussed in the comments, this is simply a matter of parsing rules. Your expression take' -2 [2] sure looks like it should mean take' (-2) [2] as you intend. And arguably, it should be parsed as just that. In fact GHC has an extension to achieve that behaviour:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XNegativeLiterals 
Prelude> take -2 [2]
[]

By default however, Haskell always first tries to parse all operators, including -, as infix operators. In the above expression, - has both something to the left and to the right (albeit with inconsistent spacing, but that's ignored), so without -XNegativeLiterals this ends up getting parsed as (take) - (2 [2]), which means something completely different. Really it's just utter bogus, as the error message suggests in a really cryptic way: it wants Num (i -> [a] -> [a]), i.e. it finds your code requires to treat a function (namely, take) as a number (namely, as an argument to the subtraction operator).

Almost always when you see an error including demand for Num (Some Compound Type) it means something is completely wrong already at the parsing level.



来源:https://stackoverflow.com/questions/41107452/type-error-when-testing-a-function-with-a-negative-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!