[Haskell-beginners] Precedence of type annotation operator ::

Daniel Fischer daniel.is.fischer at googlemail.com
Mon Dec 3 02:23:54 CET 2012


On Sonntag, 2. Dezember 2012, 22:46:58, José Romildo Malaquias wrote:
> Hello.
> 
> What is the exact precedence of the type annotation binary opertor :: ?

It's not really a binary operator.
It's part of the syntax, so it has no exact precedence, but since you're 
asking about it, I presume you're not interested in type declarations

foo :: Int -> Double
foo = sin . fromIntegral

but rather in expression type signatures. The production in the context-free 
syntax is

exp → infixexp :: [context =>] type

so the signature is for the entire infix expression:

Prelude> toEnum . floor $ 12.7 + toEnum 73 :: Char
'U'

hence if it had a precedence, it would be below 0 (the precedence of ($)).

But be aware that

"The grammar is ambiguous regarding the extent of lambda abstractions, let 
expressions, and conditionals. The ambiguity is resolved by the meta-rule that 
each of these constructs extends as far to the right as possible."

thus

Prelude> (\x -> x + x :: Int -> Int) 2

<interactive>:16:10:
    No instance for (Num (Int -> Int)) arising from a use of `+'
    Possible fix: add an instance declaration for (Num (Int -> Int))
    In the expression: x + x :: Int -> Int
    In the expression: \ x -> x + x :: Int -> Int
    In the expression: (\ x -> x + x :: Int -> Int) 2

the type signature here extends only over the `x + x`, since it is parsed as a 
part of the lambda abstraction

[ \x -> (x + x :: Int -> Int) extends farther to the right than just
 \x -> x + x]

So if you want to give a type signature to a lambda abstraction, you need 
explicit parentheses:

Prelude> ((\x -> x + x) :: Int -> Int) 2
4



More information about the Beginners mailing list