Ruta graveolens  ·  notes from a language experiment  ·  cultivated since 2025

Logical Operators

Logical operators operate on bool values and produce bool results.

Logical NOT

The logical NOT operator ! takes a single bool operand and produces a bool: the result is true when the operand is false and false when the operand is true (core calculus docs/formal/01-core-calculus.md §6.4: not true → false, not false → true).

fn main() -> i32 {
    let a = !false;   // true
    let b = !true;    // false
    let c = !!true;   // true (double negation)
    if a { 1 } else { 0 }
}

Logical AND

The logical AND operator && returns true if both operands are true.

The && operator uses short-circuit evaluation: if the left operand is false, the right operand is not evaluated. This short-circuiting is why the core calculus carries no && form of its own: elaboration removes the sugar by lowering a && b to an if that evaluates the right operand only on the branch the left operand selects (core calculus docs/formal/01-core-calculus.md §1; the taken-branch-only evaluation is §6.2).

fn main() -> i32 {
    if true && true { 1 } else { 0 }   // 1
    if true && false { 1 } else { 0 }  // 0
}

Logical OR

The logical OR operator || returns true if either operand is true.

The || operator uses short-circuit evaluation: if the left operand is true, the right operand is not evaluated. As with &&, the core calculus carries no || form: elaboration lowers a || b to an if that evaluates the right operand only on the branch the left operand selects (core calculus docs/formal/01-core-calculus.md §1; the taken-branch-only evaluation is §6.2).

fn main() -> i32 {
    if false || true { 1 } else { 0 }  // 1
    if false || false { 1 } else { 0 } // 0
}

Precedence

Operator precedence (highest to lowest):

  1. ! (logical NOT)
  2. && (logical AND)
  3. || (logical OR)
fn main() -> i32 {
    // true || false && false => true || (false && false) => true
    if true || false && false { 1 } else { 0 }
}

Type Checking

All operands of logical operators MUST have type bool.