Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the precedence of `->`, `=` and ` ` in Haskell?

I'm trying to figure out some default operator precedences in Haskell, but I was unable to find some good documentation on ->, = and (as in f x). So I tried :i (->) and :i (=) in GHCI to get some info, but it gives me a syntax error.

Apparently these "tokens" are just a built-in part of the syntax, so no wonder, that :i doesn't work.

I'm new to Haskell, so I wasn't aware of the fact, that = doesn't return any value, I just mistakingly assumed, that it behaves as its equivalents in imperative languages, which is wrong of course.

-> and , on the other hand, behave as operators. They return a type/value and are right/left associative respectively. And they have some sort of perecedence when used along with actual operators.

like image 447
egst Avatar asked May 18 '19 21:05

egst


People also ask

How precedence work?

An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first. Precedence can also be described by the word "binding." Operators with a higher precedence are said to have tighter binding.

How does ++ work in Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combine" them into a single list. So if you have the list [x] and the list [y] then you can concatenate them like this: [x]++[y] to get [x, y ]. Notice that : takes an element and a list while ++ takes two lists.

Is there and operator in Haskell?

In Haskell we have or operator to compare the values of the variable, this operator also comes under the lexical notation of the Haskell programming language. This operator works in the same way as any other programming language, it just returns true or false based on the input we have provided.

What does op do in Haskell?

Op provides operators for writing easier-to-read Haskell. It provides new operators with a consistent "look and feel" including fixity direction and precedence, resulting in easier- and quicker-to-read code especially when used on long chains of expressions.


1 Answers

  • -> is a type-level operator ((->) :: * -> * -> *), and as mentioned in comments, :i (->) reveals that it is infixr 0*.
  • Function application could be seen as having 'infinitely high' left precedence, that is to say if % is any operator, then f x % y will always be read as (f x) % y no matter what precedence % has, and f x y z is always read as ((f x) y) z.** This isn't documented as having a precedence, because it isn't an operator, and 'infinite' precedence can't be declared in Haskell.
  • = cannot be seen as having precendence, as it is always declaration rather than an expression, so putting parentheses around it is absurd. It is not an operator, hence cannot have precedence.

* As pointed out in a below comment, this actually behaves as if it has precedence infixr -1, but this is not permitted in ordinary operators — this is syntactic rather than semantic.

** Note that this is the 'opposite' of ->, which could be seen as having 'infinitely low', right precedence. Can you see why this is natural?

like image 77
AJF Avatar answered Oct 18 '22 02:10

AJF