Skip to content

Instantly share code, notes, and snippets.

@thewoolleyman
Last active January 6, 2023 12:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewoolleyman/cb89b77ecc45038cc36c15f0573d7107 to your computer and use it in GitHub Desktop.
Save thewoolleyman/cb89b77ecc45038cc36c15f0573d7107 to your computer and use it in GitHub Desktop.
Haskell Precedence and Associativity

Haskell Precedence and Associativity

Operator precedence vs. operator associativity:

Operator Precedence

...describes the nesting order of compound expressions of different operator types.

Operator Associativity

...describes the nesting order of compound expressions with the same operator precedence

Why are logical operators in JavaScript left associative? - http://stackoverflow.com/questions/20591876/why-are-logical-operators-in-javascript-left-associative/20754697#20754697

Evaluation vs. Parsing

"Associativity, like precedence, is not about what evaluates first, it is about how the expression is parsed."

The distinction between parsing and evaluation is important.

https://www.quora.com/How-does-one-explain-the-right-to-left-associativity-of-the-conditional-operator-in-C

Function Application vs. Function Definition

Finally, the function application "operator" (i.e., the space between arguments in a function call) associates to the left, while the function type-mapping infix operator in a function definition (i.e. -> ) associates to the right. And "associates to the right/left" means "the things on that side are parsed, (not evaluated), first".

https://www.haskell.org/tutorial/functions.html

From the first section of that tutorial page:

First, consider this definition of a function which adds its two arguments:

add :: Integer -> Integer -> Integer

add x y = x + y

An application of add has the form add e1 e2, and is equivalent to (add e1) e2, since function application associates to the left. In other words, applying add to one argument yields a new function which is then applied to the second argument. This is consistent with the type of add, Integer->Integer->Integer, which is equivalent to Integer->(Integer->Integer); i.e. -> associates to the right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment