Questions tagged [associativity]

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in `a+b+c-d`.

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in a+b+c-d.

See the Wikipedia page on operator associativity in programming languages.

Note: the concept is related to the associative property of operators in mathematics, but there are some subtle differences. Compare with Wikipedia page on associative property.

A common problem when first learning to parse infix operators using recersive descent is that one will get expressions such as 1+2 or 3-1 or 3*4 or 6/3 to work. Then they will try using multiple sequential of an operator such 1+2+3 which will work but find that 7-3-2 which is equivalent to (7-3)-2 which is 2 will not work and typically get 6 because they have parsed it as 7-(3-2) which is incorrect.

For solutions to this problem
See: Parsing Expressions by Recursive Descent by Theodore Norvell

185 questions
3
votes
0 answers

Why do right associative extension methods have the opposite target from normal methods?

I've read all of the reference docs and expanded explanation, and have seen the related question. While I understand the need to re-swap the operands for the curried arguments on the extension method, there is still a practical behavior difference I…
dbingham
  • 131
  • 2
3
votes
1 answer

Optimizing Recursive Function in Java

I'm having problem with optimizing a function AltOper. In set {a, b, c}, there is a given multiplication (or binomial operation, whatsoever) which does not follow associative law. AltOper gets string consists of a, b, c such as "abbac", and…
3
votes
1 answer

Right way to parse chain of various binary functions with `Parsec`?

It is true that Parsec has chainl and chainr to parse chains of either left-associative or right-associative operations (i.e. a -> a -> a). So I could quite easily parse something like x + y + z in a ((a + y) + z) or (a + (y + z)) manner. However,…
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
3
votes
2 answers

Is there a quick way to determine precedence and associativity of operators?

I know about perlop. What I am looking for is a quick lookup like the GHCi :info command: ghci> :info (+) class (Eq a, Show a) => Num a where (+) :: a -> a -> a ... -- Defined in GHC.Num infixl 6 + where I learn (+) is left-associative…
matthias krull
  • 4,389
  • 3
  • 34
  • 54
3
votes
0 answers

Proof of associativity law for type-level set

I'm trying to prove that type-level function Union is associative, but I'm not sure how it should be done. I proved right identity and associativity laws for type-level append function and right identity for union: data SList (i :: [k]) where SNil…
starper
  • 175
  • 1
  • 8
3
votes
1 answer

Why is using reduce with non-associative operators considered bad style?

As Guido says in his The fate of reduce() in Python 3000 post: So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial…
Doug Coburn
  • 2,485
  • 27
  • 24
3
votes
2 answers

Coq: Remove all (nested) parentheses from a sum

Suppose I've a sum like a + (b + (c + d)), which I wish to transform into a + b + c + d to apply a lemma. Doing this manually with Nat.add_assoc is extremly tedious. Is there a smarter way?
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
3
votes
1 answer

What is the precedence among operators in XPath?

In this XPath expression: //div[@id=”myID”]|p, does the // operator get applied to both sides of the union operator? Or would this expression simply return all div elements in the document that have an id attribute value of myID and all p elements…
Clint
  • 900
  • 11
  • 25
3
votes
1 answer

Ternary Operator Associativity

I am having trouble understanding the concept of associativity in the context of ternary operators. In most cases, ternary operators look like this: a ? b : c In this case, no associativity is needed to evaluate the expression. Sometimes though,…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
3
votes
4 answers

Associativity in Lambda calculus

I am working on the exercise questions of book The Lambda calculus. One of the questions that I am stuck is proving the following: Show that the application is not associative; in fact, x(yz) not equals (xy)z Here is what I have worked on so…
name_masked
  • 9,544
  • 41
  • 118
  • 172
3
votes
2 answers

C++: Associativity of * (multiply) operator is not left-to-right

While working on a school assignment, we had to do something with operator overloading and templates. All cool. I wrote: template class Multiplication : public Expression { private: typename std::shared_ptr > l,…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
3
votes
1 answer

Native implementation of reduceRight in JavaScript is wrong

For an associative operation f over the elements of array a, the following relation should hold true: a.reduce(f) should be equivalent to a.reduceRight(f). Indeed, it does hold true for operations that are both associative and commutative.…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
3
votes
3 answers

How does the different behavior of the unless- and "if !" statement influence the range-operator in scalar context?

On http://novosial.org/perl/one-liner/ I found the following two one-liners. The outputs are different because the unless statement is different from if ! ( due to the associativity and precedence rules ). cat file: foo bar perl -ne…
sid_com
  • 24,137
  • 26
  • 96
  • 187
3
votes
2 answers

What do you mean by left to right associativity in this case?

I was reading about the << operator in c++ from C++ Primer. In it, there are some lines written about << operator cout << "Some string" evaluates to a left hand ostream operator that is the result of the expression is cout object itself.(We say…
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
3
votes
1 answer

Associative, commutative properties and identity elements of non-binary functions

I'm making a compiler (for a new language) wich supports AC unification via pattern matching. The matching algorithms already works but i'm having trouble with the logical and mathematical aspects of functions and it's properties, wich will define…
Rafael
  • 2,642
  • 2
  • 24
  • 30