# » Language: Boolean Expressions

Boolean expressions are expressions that evaluate to a boolean value from the result of a combination of one or more comparisons and logical operators. Boolean expressions form the basis of policy since policy can be broken down to a set of logical decisions that turn into true or false.

A single boolean expression is the body of a rule. Additionally, boolean expressions are used with conditionals, and more.

## » Order of Operations

The precedence of operators is shown below. Operators with higher precedence values (larger numbers) are evaluated first. Operators with the same precedence value are evaluated left-to-right.

Examples of this precedence are shown below:

## » Logical Operators

Logical operators are used to change or combine logical expressions.
There are three binary operators `and`

, `or`

, and `xor`

. There is
a single unary operator `not`

(which can also be specified as `!`

).

The binary operators require two boolean operands and the unary operator requires a single boolean operand. In both case, the operands are values or expressions that are boolean types.

Below is a basic explanation of the logical operators directly from the specification:

Using parentheses to group boolean expressions, you can combine boolean expressions to become more complex or affect ordering:

## » Comparison Operators

Comparison operators compare two operands and yield a boolean value.

For any comparison, the two operands must be equivalent types. The one exception are integers and floats. When an integer is compared with a float, the integer is promoted to a floating point value.

The available comparison operators are:

The behavior of `is`

with `==`

and `is not`

with `!=`

is identical.

Example comparisons:

Using the logical operators, these can be combined:

The language specification provides more detail on exactly how comparison behaves.

## » Set Operators

The set operators `contains`

and `in`

test for inclusion in a collection
(a list or map).

Set operators may be negated by prefixing the operator with `not`

, such
as `not contains`

and `not in`

. This is equivalent to wrapping the expression
in a unary `not`

but results in a more readable form.

`contains`

tests if the left-hand collection contains the right-hand value.
`in`

tests if the right-hand collection contains the left-hand value. For
maps, "contains" looks at the keys, not the values.

Examples:

## » Matches Operator

The `matches`

operator tests if a string matches a regular expression.
The `matches`

operator can be negated by prefixing the operator with
`not`

, such as `not matches`

.

The left-hand value is the string to test. The right-hand value is a string value representing a regular expression. The syntax of the regular expression is the same general syntax used by Python, Ruby, and other languages. The precise syntax accepted is the syntax accepted by RE2.

Regular expressions are not anchored by default; any anchoring must be explicitly specified.

## » Any, All Expressions

`any`

and `all`

expressions allow testing that any or all elements of a
collection match some boolean expression. The result of an `any`

and `all`

expression is a boolean.

`any`

returns the boolean value `true`

if any value in the collection expression
results in the body expression evaluating to `true`

. If the body expression
evalutes to `false`

for all values, the any expression returns `false`

.

`all`

returns the boolean `true`

if all values in the collection expression
result in the body expression evaluating to `true`

. If any value in the
collection expression result in the body expression evaluating to `false`

,
the `all`

expression returns `false`

.

For empty collections, `any`

returns `false`

and `all`

returns `true`

.

Since `any`

and `all`

expressions are themselves boolean expressions, they
can be combined with other operators:

## » Emptiness Comparison

The expressions `is empty`

and `is not empty`

provide a convenience
method for determining the emptiness of a value. It supports the same types
as the built-in length, collections and strings.