Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

haskell - Type error when testing a function with a negative number

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...