Type error when testing a function with a negative

2020-02-15 03:32发布

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条回答
叛逆
2楼-- · 2020-02-15 04:07

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.

查看更多
登录 后发表回答