Operatorassoziativität

Operatorassoziativität bezeichnet vor allem in der Informatik, aber auch Mathematik und Logik die Festlegung, wie komplexere Ausdrücke mit infix-Operatoren, die für zweistellige Operationen stehen, die nicht unbedingt assoziativ sind, zu lesen sind.

Zum Beispiel sind in der Mathematik die Addition und Multiplikation, die üblicherweise mit + {\displaystyle +} bzw. {\displaystyle \cdot } notiert werden, assoziative Operationen, es gilt also ( a + b ) + c = a + ( b + c ) {\displaystyle (a+b)+c=a+(b+c)} bzw. ( a b ) c = a ( b c ) {\displaystyle (a\cdot b)\cdot c=a\cdot (b\cdot c)} , ebenso wie in der Logik die Konjunktion ( ) {\displaystyle (\land )} und Disjunktion ( ) {\displaystyle (\lor )} . Hier ist es gleichgültig, ob a + b + c {\displaystyle a+b+c} als ( a + b ) + c {\displaystyle (a+b)+c} oder als a + ( b + c ) {\displaystyle a+(b+c)} interpretiert wird.

Bei nicht assoziativen Operationen, wie etwa der Subtraktion (infix notiert durch „–“), gilt a ( b c ) = ( a b ) c {\displaystyle a-(b-c)=(a-b)-c} nicht allgemein, und daher muss festgelegt werden, ob Ausdrücke wie a b c {\displaystyle a-b-c} überhaupt erlaubt sind, und, falls sie erlaubt sind, welche Klammerung implizit vorliegen soll. Wird festgelegt, dass „–“ linksassoziativ ist, ist a b c {\displaystyle a-b-c} wie ( a b ) c {\displaystyle (a-b)-c} zu interpretieren. Soll „–“ dagegen rechtsassoziativ sein, wäre a b c {\displaystyle a-b-c} wie a ( b c ) {\displaystyle a-(b-c)} zu interpretieren.

Linksassoziative Operatoren

Bei linksassoziativen Operatoren wird implizite Linksklammerung vereinbart[1][2][3][4][5] – ein binärer Operator {\displaystyle *} gilt somit als linksassoziativ, wenn die Ausdrücke

a b c {\displaystyle a*b*c} := ( a b ) c {\displaystyle :=(a*b)*c}
a b c d {\displaystyle a*b*c*d} := ( ( a b ) c ) d {\displaystyle :={\bigl (}(a*b)*c{\bigr )}*d}
etc.

wie gezeigt zu lesen sind. Beispiele für linksassoziative Operatoren sind:

  • Subtraktion, „–“:
a b c {\displaystyle a-b-c} = ( a b ) c {\displaystyle =(a-b)-c}
  • Division, „:“, „/“:
a : b : c {\displaystyle a:b:c} = ( a : b ) : c {\displaystyle =(a:b):c}
a ÷ b ÷ c {\displaystyle a\div b\div c} = ( a ÷ b ) ÷ c {\displaystyle =(a\div b)\div c}
a / b / c {\displaystyle a/b/c} = ( a / b ) / c {\displaystyle =(a/b)/c}
      Jedoch: Bei waagerechten Bruchstrichen bindet der kürzere Bruchstrich stärker:
a b c {\displaystyle {\frac {a}{\frac {b}{c}}}} = a ( b c ) = a c b {\displaystyle ={\frac {a}{{\bigl (}{\frac {b}{c}}{\bigr )}}}={\frac {a\cdot c}{b}}}
a b c {\displaystyle {\frac {\frac {a}{b}}{c}}} = ( a b ) c = a b c {\displaystyle ={\frac {{\bigl (}{\frac {a}{b}}{\bigr )}}{c}}={\frac {a}{b\cdot c}}}
  • Funktionsanwendung durch Juxtaposition in vielen Programmiersprachen, u. a. Haskell:
f x y z = ((f x) y) z.

Rechtsassoziative Operatoren

Umgekehrt liegt bei rechtsassoziativen Operatoren {\displaystyle *} implizite Rechtsklammerung vor, so dass gilt:

x y z {\displaystyle x*y*z} := {\displaystyle :=} x ( y z ) {\displaystyle x*(y*z)}
w x y z {\displaystyle w*x*y*z} := {\displaystyle :=} w ( x ( y z ) ) {\displaystyle w*(x*(y*z))}
etc.

Beispiele für rechtsassoziative Operatoren sind:[6]

  • Die Potenzierung: x y z := x ( y z ) {\displaystyle x^{y^{z}}:=x^{(y^{z})}} , denn ( x y ) z {\displaystyle (x^{y})^{z}} wäre einfach x y z {\displaystyle x^{yz}} .
    Achtung: Taschenrechner werten Eingaben der Form x ^ y ^ z gleichwohl in der Regel linksassoziativ, also so aus, als ob sie in der Form (x ^ y) ^ z eingegeben worden wären – bei Ausdrücken dieser Form muss daher die Rechtsassoziativität der Potenzierung stets mittels eigener Klammersetzung erzwungen werden: x ^ (y ^ z).
  • Die Subjunktion in der Logik wird von den meisten Autoren rechtssassoziativ verwendet, das heißt, dass P Q R {\displaystyle P\rightarrow Q\rightarrow R} als P ( Q R ) {\displaystyle P\rightarrow (Q\rightarrow R)} zu lesen ist.
  • Der Zuweisungsoperator einiger Programmiersprachen, wie C: x = y = z ist gleichbedeutend mit x = (y = z), das heißt, der Variablen y wird zunächst der Wert von z zugewiesen und erst danach das Ergebnis dieser Zuweisung (also der zugewiesene Wert z) der Variablen x zugewiesen.
  • Funktionsanwendung durch infix-$ in Haskell:
f $ g $ h $ x = f $ (g $ (h $ x)).

Weder, noch

Es kann auch sein, dass Ausdrücke wie a b c {\displaystyle a\bullet b\bullet c} einfach verboten werden, selbst dann, wenn die Operation, für die der Operator {\displaystyle \bullet } steht, assoziativ ist. So ist zum Beispiel in Haskell der Vergleichsoperator ==, wie auch <=,> usw., in diesem Sinne „nicht-assoziativ“, obwohl die Vergleichsoperation zwischen Booleschen Werten etwa (als Funktion 2 × 2 2 {\displaystyle 2\times 2\to 2} ) assoziativ ist.

Siehe auch

  • Operatorrangfolge

Einzelnachweise

  1. Order of operations. (Memento vom 5. September 2017 im Internet Archive) (PDF; 265 kB) Rochester Institute of Technology
  2. The Order of Operations. Education Place
  3. The Order of Operations. Khan Academy (Video, ab 05:40)
  4. Using Order of Operations and Exploring Properties. (PDF; 268 kB) Absatz 9, Virginia Department of Education
  5. Vorrangregeln und Assoziativität. Technische Universität Chemnitz
  6. Rules for Exponents and the Reasons for Them. (PDF; 344 kB) Western Michigan University