# Precedence

``````Expr := "{" Expr "}"
Expr := "(" Expr ")"``````

Precedence can be indicated with either curly braces or parentheses. When used for this purpose, the two are exactly equivalent.

For readability, it can be helpful to always use `(...)` for unary singleton relations (scalars) and `{...}` for relations that are not singletons. For example, `(x + y) * z` versus `{x, y, z: p(x, y, z)}`.

All binary operators associate to the left, unless otherwise stated below. They bind an operand to the left operation when the right operation has the same precedence. For example, ` R . S . T` is equivalent to `(R . S) . T`.

There are a few right-associated operations like `^` or `implies`. They — as opposed to left-associated operations — bind operands to the right operation when the left operation has the same precedence. Right-associated operations are explicitly called out. For example, `2^3^2` is equivalent to `2^(3^2) = 2^9 = 512` and not `(2^3)^2 = 2^6 = 64`.

The list of the operator precedence from highest to lowest:

1. `_`, variables, names of relations, qualified names, literals, `()`, and `{}`.
2. `.`.
3. Relational application and partial relational application.
4. `^` (associates to the right).
5. Unary `-`.
6. `/`, `%`, `*`, `÷`, `×`, `⊗`, and `⊙`.
7. `-`, `+`, `⊕`, `∩`, and `⊓`.
8. `∪` and `⊔`.
9. `=`, `!=`, `≈`, `∼`, `≠`, `<`, `>`, `<=`, `≤`, `>=`, `≥`, `⊆`, `⊇`, `⊂`, `⊃`, `≼`, `≽`, `≺`, `≻`, `→`, and `←`.
10. `¬` and `not`.
11. `forall`, `∀`, `exists`, and `∃`.
12. `∧` and `and`.
13. `∨` and `or`.
14. `⇒` and `implies` (both associate to the right).
15. `⇐`.
16. `<:` and `:>`.
17. `<++` and `++>`.
18. `,`.
19. `;`.
20. `≡`, `⇔`, and `iff` (all associate to the right).
21. `≢`,`⇎`, `⊻`, and `xor` (all associate to the right).
22. `:` (associates to the right).

Note that relational application and partial relational application are created with an implicit operator. The precedence of this operator is lower than that of the composition operator `.`, so an expression such as `P . Q[x]` is parsed as `(P . Q)[x]`. If this is not what you want, you should use explicit parentheses, for example: `P . (Q[x])`.

The following example illustrates this:

``````// read query

def P = (1, 2)
def Q = {("a", 1); ("a", 2)}

def output:A = Q . P

def output:B   = Q . P["a"]
def output:BB  = (Q . P)["a"]
def output:BBB = Q . (P["a"])

def output:C   = Q . P
def output:CC  = (Q . P)
def output:CCC = Q . (P)

def output:D   = Q . P("a", 2)
def output:DD  = (Q . P)("a", 2)
def output:DDD = Q . (P("a", 2))``````

Next: Relation Literals