2
votes

In GHCi, if I define two functions like so:

> let succ = (+ 1)
> let pred = (- 1)

And then call them both, like so:

> succ 5
> pred 5

Why does one (succ) run just fine, while the other fails with the following error?

<interactive>:3:1:
Could not deduce (Num (a0 -> t))
  arising from the ambiguity check for ‘it’
from the context (Num (a -> t), Num a)
  bound by the inferred type for ‘it’: (Num (a -> t), Num a) => t
  at <interactive>:3:1-6
The type variable ‘a0’ is ambiguous
When checking that ‘it’
  has the inferred type ‘forall a t. (Num (a -> t), Num a) => t’
Probable cause: the inferred type is ambiguous

What is causing the type inference to fail in one function and not the other?

1

1 Answers

7
votes

- has a weird special syntax rule in Haskell. It's the only infix that can't be used as a left-infix section, instead -x will always be parsed as a single negative number. The symbol is only recognised as an infix when

  • used explicitly as subtraction, 2-1
  • used as a right section, (1-)
  • used as a dyadic prefix function, (-) 1.