nustack

logical

Logical atoms: Python's boolean operators and truthiness.

Module nu.core.logical.

Logical atoms: Python's boolean operators and truthiness.

Maps Python's boolean operations onto Nu ScalarQueries. Pure compute; no Context effect of their own.

Builtins / operators to cover (Python -> Nu):

  • and -> And, or -> Or, not -> Not
  • bool (truthiness) -> ToBool

Sorts: all ScalarQuery (Q). And / Or are variadic; Not and ToBool are unary. logical owns ToBool; cast does not define it.

And / Or semantics: Python's and / or short-circuit and return an operand (not a bool). Nu short-circuits the same way but always coerces to bool, so a Nu And / Or yields a plain boolean while leaving the operands past the deciding one unevaluated. That is what makes them usable as guards: And(not_empty(x), contains(x)) never runs contains when x is empty. And yields True over no operands, Or yields False.

Sentinels: And / Or decide on truthiness, so an EMPTY operand is just a falsy one (per nu.lang.sentinels) and never poisons the result. Not and ToBool are queries over a value and propagate EMPTY.

NameSortCallMeaning
Andscalar_queryAnd(*children)The conjunction of its boolean children, each coerced with bool.
Notscalar_queryNot(value)The negation of its one child.
Orscalar_queryOr(*children)The disjunction of its boolean children, each coerced with bool.
ToBoolscalar_queryToBool(value)The truthiness of its one child.
boolcore.bool(x)Coerce x to a Nu Bool term.

And

The conjunction of its boolean children, each coerced with bool.

And(*children)

Path nu.core.And. Kind ScalarQuery, sort scalar_query, cardinality scalar. Arity None (0 required).

Arguments

NameTypeDefaultMeaning
*childrenthe values to conjoin.

Yields

A plain bool, never EMPTY.

Notes

  • Short-circuits like Python's and: the first falsy child decides the result and the children after it are never evaluated. This is what lets And guard - a guard that still runs the thing it is guarding is not a guard.
  • An EMPTY child counts as false, like any falsy value.
  • No children at all yields True.

Example

nu.run(nu.And(True, True))[0]
nu.run(nu.And(True, False))[0]
True
False

Not

The negation of its one child.

Not(value)

Path nu.core.Not. Kind ScalarQuery, sort scalar_query, cardinality scalar. Arity 1 (1 required).

Arguments

NameTypeDefaultMeaning
valuethe value to negate.

Yields

A plain bool. EMPTY when the child is EMPTY.

Example

nu.run(nu.Not(True))[0]
False

Or

The disjunction of its boolean children, each coerced with bool.

Or(*children)

Path nu.core.Or. Kind ScalarQuery, sort scalar_query, cardinality scalar. Arity None (0 required).

Arguments

NameTypeDefaultMeaning
*childrenthe values to disjoin.

Yields

A plain bool, never EMPTY.

Notes

  • Short-circuits like Python's or: the first truthy child decides the result and the children after it are never evaluated. This is what lets Or guard - a guard that still runs the thing it is guarding is not a guard.
  • An EMPTY child counts as false, like any falsy value.
  • No children at all yields False.

Example

nu.run(nu.Or(False, True))[0]
nu.run(nu.Or(False, False))[0]
True
False

ToBool

The truthiness of its one child.

ToBool(value)

Path nu.core.ToBool. Kind ScalarQuery, sort scalar_query, cardinality scalar. Arity 1 (1 required).

Arguments

NameTypeDefaultMeaning
valuethe value to coerce.

Yields

A plain bool. EMPTY when the child is EMPTY.

Example

nu.run(nu.core.logical.ToBool(0))[0]
False

bool

Coerce x to a Nu Bool term.

core.bool(x)

Path nu.core.bool. Defined on nu.core.logical, bound as a function. Builds object.

Arguments

NameTypeDefaultMeaning
xobjectthe value to coerce.

Yields

A Nu Bool.

Notes

  • Bool(ToBool(x)) in one call: Form-wraps the raw ToBool atom so the result composes like any other Nu term.

Example

nu.run(nu.core.logical.bool(1))[0]
True

On this page