Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the infix priority of type annotation (::)

Tags:

What's the priority of ::, is there any operator has even lower priority than it?

It seems the priority of :: is lower than $, because both of the expression below returns "ab"

map head $ ["alice", "bob"] :: String
map head ["alice", "bob"] :: String
like image 267
Leo Zhang Avatar asked Jun 21 '18 05:06

Leo Zhang


People also ask

What is infix and postfix notation with example?

Infix, Prefix and Postfix expression with example 1 Infix notation: A + B. When we write any arithmetic expression in infix notation, operators are written in-between their operands. 2 Postfix notation (“Reverse Polish notation”): A B +. ... 3 Prefix notation (“Polish notation”): + A B. ... 4 Point to be consider while Parsing Expression. ...

What are prefix and postfix notions in C++?

Prefix and postfix notions are methods of writing mathematical expressions without parentheses. Let’s see the infix, postfix and prefix conversion. In infix expressions, the operator precedence is implicit unless we use parentheses. Therefore, we must define the operator precedence inside the algorithm for the infix to postfix conversion.

What is the Order of evaluation of operators in postfix notation?

When we write any arithmetic expression in Postfix notation, operators are written after their operands. The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the “+” is to the left of the “*” in the example above, the addition must be performed before the multiplication.

What is the Order of priority of the operators?

The operators are listed in order of priority, group 1 having the highest priority and group 7 the lowest. All operators in the same priority group have the same priority. For example, the exponentiation operator ** has the same priority as the prefix + and prefix - operators and the not operator ¬.


Video Answer


1 Answers

The :: part is called a type annotation.

A message on the mailing list points to exactly the same conclusion, supported by the grammar:

https://mail.haskell.org/pipermail/beginners/2012-December/011017.html

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
like image 145
Koterpillar Avatar answered Sep 20 '22 02:09

Koterpillar