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.
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.
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.
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.
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.
->
is a type-level operator ((->) :: * -> * -> *
), and as mentioned in comments, :i (->)
reveals that it is infixr 0
*.%
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With