← Back to AILP Home

Boolean — bool

Overview

The bool type in Nitpick is a distinct primitive type representing a boolean truth value: true or false. It is strictly typed and represents a fundamental building block for control flow.

Declaration & Literal Values

Boolean literals are exactly true and false.

bool:is_ready = true;
bool:is_done = false;

bool vs tbb8 distinction

Nitpick strictly separates memory/integer types from truth types. While tbb8 (an 8-bit two's complement integer with an ERR band) is 1 byte, bool is not an alias for tbb8.

There are no implicit conversions between numeric types and bool. You cannot assign an int32 or a tbb8 to a bool, nor vice versa.

// ERROR: Cannot assign numeric types to bool
// bool:b1 = 1i32; 
// bool:b2 = 0t8;

// Correct way to map from numeric to bool:
int32:x = 10i32;
bool:b3 = (x != 0i32);

Control Flow Contexts

Nitpick strictly requires a bool type for all conditional control flow structures. Truthiness (evaluating 0 as false and non-zero as true) is intentionally absent.

The if Statement

if (is_ready) {
    // executes if true
} else {
    // executes if false
}

The while Loop

while (!is_done) {
    // executes as long as is_done remains false
}

The when/then/end Construct

when (is_ready) {
    // Executes loop body
} then {
    // Executes on clean loop termination (is_ready evaluates to false)
} end {
    // Executes on early break
}

The pick Expression

pick (is_ready) {
    (true) { println("Ready!"); },
    (false) { println("Wait!"); }
}

The is Ternary

int32:status = is (is_ready) : 200i32 : 503i32;

Logical Operations

Nitpick provides three standard logical operators. Arithmetic operators (+, -, *, /) are rejected for bool types.

bool:a = true;
bool:b = false;

// Logical AND (Short-circuit)
bool:and_r = (a && b);

// Logical OR (Short-circuit)
bool:or_r = (a || b);

// Logical NOT
bool:not_r = (!a);

Short-Circuit Evaluation

Both && and || use short-circuit evaluation. This means the right-hand operand is not evaluated if the left-hand operand already determines the outcome: - For &&, if the left side is false, the expression evaluates to false without executing the right side. - For ||, if the left side is true, the expression evaluates to true without executing the right side.

Comparison Operations

Equality (==) and inequality (!=) are defined for two bool values.

bool:same = (a == b);
bool:diff = (a != b);

All relational comparisons on numeric and string types yield a bool.

bool:gt = (5i32 > 3i32);
bool:eq = (name == "Nitpick");

Data Structures & Functions

bool can be used securely inside structures, arrays, and as function parameters or return types.

struct:Flags = {
    bool:ready;
    bool:done;
};

// Array of booleans
bool[4]:arr = [true, false, true, true];

// Iteration over a boolean array
for (bool:b in arr) {
    if (b) { println("Found a true value!"); }
}

// Function parameters and return values
func:is_valid = bool(bool:input_a, bool:input_b) {
    pass input_a && input_b;
};

Debugging and Output

When printing or debugging bool values, Nitpick natively renders them as the text "true" or "false".

bool:active = true;
println(active);          // Output: true
dbug.check(active);       // Output: CHECK: true

Template Literal Interpolation

When interpolating a bool into a template literal, Nitpick correctly formats the value to "true" or "false".

bool:active = true;
string:log = `Status active: &{active}`; // Yields "Status active: true"

Formal Semantics

The bool type is covered in the k-semantics/nitpick.k executable formal model. - A dedicated Bool sort distinguishes it from Int in memory. - if, while, pick, and when rules mandate Bool typed conditions. - Logical operators correctly perform short-circuit reductions across the K <k> cell.

Best Practices

// DON'T: Compare directly to boolean literals
// if (is_ready == true) { ... }
// if (is_ready != false) { ... }

// DO: Use the boolean directly or with NOT
// if (is_ready) { ... }
// if (!is_ready) { ... }

Related Types