Skip to content

Commit

Permalink
Short replacement for non-normative section on mathematical associati…
Browse files Browse the repository at this point in the history
…vity
  • Loading branch information
henrikt-ma committed Mar 2, 2022
1 parent 075f5e4 commit c6c06a6
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions chapters/operatorsandexpressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ \chapter{Operators and Expressions}\label{operators-and-expressions}
Expressions can contain variables and constants, which have types, predefined or user defined.
The predefined built-in types of Modelica are \lstinline!Real!, \lstinline!Integer!, \lstinline!Boolean!, \lstinline!String!, and enumeration types which are presented in more detail in \cref{predefined-types-and-classes}.


\section{Expressions}\label{expressions}

Modelica equations, assignments and declaration equations contain expressions.
Expand All @@ -20,6 +21,7 @@ \section{Expressions}\label{expressions}
The function call syntax for both positional and named arguments is described in \cref{positional-or-named-input-arguments-of-functions} and for vectorized calls in \cref{initialization-and-binding-equations-of-components-in-functions}.
The built-in array functions are given in \cref{array-dimension-lower-and-upper-index-bounds} and other built-in operators in \cref{built-in-intrinsic-operators-with-function-syntax}.


\section{Operator Precedence and Associativity}\label{operator-precedence-and-associativity}

Operator precedence determines the implicit subexpression structure of expressions with operators.
Expand All @@ -35,16 +37,8 @@ \section{Operator Precedence and Associativity}\label{operator-precedence-and-as
Note that the operators don't need to be identical for associativity to matter; also \lstinline!1 == 2 < 3! is invalid, and \lstinline!1 - 2 + 3! is implicitly structured as \lstinline!(1 - 2) + 3!.
Also note that the non-associative array range in Modelica can be used with either two or three operands separated by `\lstinline!:!', meaning that \lstinline!1 : 2 : 5! is one valid ternary use of the operator rather than two invalid binary uses of the operator.

\begin{nonnormative}
Associativity of a precedence group is a property of the Modelica operator symbols, and is not the same concept as mathematical operator associativity.
In the mathematical sense, none of the Modelica operator symbols are associative.
For example, \lstinline!a * (b * c)! cannot directly be interpreted as \lstinline!a * b * c! (which has the implicit subexpression structure \lstinline!(a * b) * c!).

Mathematical associativity is a property of the mathematical operations to which the Modelica operator symbols get mapped after the operand types have been determined.
For example, if \lstinline!a!, \lstinline!b! and \lstinline!c! are all \lstinline!Real! (scalar) expressions, then the Modelica operator `\lstinline!*!' is mapped to multiplication of real numbers, which is an associative and commutative operator in the mathematical sense.
As explained in \cref{evaluation-order}, this means that a tool is internally allowed to reorder the factors freely.
On the other hand, if \lstinline!a! is a \lstinline!Real! (scalar) while \lstinline!b! and \lstinline!c! are \lstinline!Real[3]! vectors, then the first Modelica operator `\lstinline!*!' is mapped to scalar multiplication, while the second `\lstinline!*!' is mapped to vector scalar product, so although both are associative and commutative in the mathematical sense, they are not the same operation and it is clear that the meaning of \lstinline!a * b * c! (and hence \lstinline!(a * b) * c!) would be different.
\end{nonnormative}
Note that at the parsing stage -- which is where the here defined operator precedence and associativity matters -- the subexpression structure is fixed.
Since Modelica tools have the freedom to symbolically manipulate expressions, this subexpression structure cannot be expected to reflect order of evaluation, compare \cref{evaluation-order}.

The following table presents the precedence and associativity of all the expression operators, consistent with and complementing information that can be derived from the Modelica grammar in \cref{modelica-concrete-syntax}.
\begin{table}[H]
Expand Down

0 comments on commit c6c06a6

Please sign in to comment.