cana.function_outputs.expression module

Positional Boolean expression helpers for BooleanFunction.

In-memory peer IR uses python and/or/not over x0…x{k-1} (MSB = x0 = inputs[0]). Named source strings are translated at the I/O / BooleanNode boundary.

Naming: - *_positional* / named_to_positional / positional_to_named —

fixed IR kind (positional x_i or named inputs).

cana.function_outputs.expression.normalize_operators(expr, style='python')[source]

Normalize boolean operators to and/or/not or &/|/!.

Works on positional or named expressions (operators only). Prefix ~ is treated as logical NOT (same as ! / not), not Python bitwise complement.

Parameters:
Return type:

str

cana.function_outputs.expression.expression_identifiers(expr)[source]

Return identifier tokens in expr, excluding boolean keywords.

Works on positional (x0…) or named expressions.

Parameters:

expr (str)

Return type:

List[str]

cana.function_outputs.expression.input_names_in_expression(expr, known_names)[source]

Select known node names that appear as tokens in a named expression.

known_names order is preserved. Intended for named (input) expressions, not positional x_i IR.

Parameters:
  • expr (str)

  • known_names (Sequence[str])

Return type:

List[str]

cana.function_outputs.expression.positional_names(k)[source]

Return ['x0', 'x1', …, 'x{k-1}'].

Parameters:

k (int)

Return type:

List[str]

cana.function_outputs.expression.named_to_positional(expr, input_names)[source]

Rewrite a named expression to positional x0…x{k-1} IR.

Parameters:
  • expr (str)

  • input_names (Sequence[str])

Return type:

str

cana.function_outputs.expression.positional_to_named(expr, input_names)[source]

Rewrite positional x_i IR to a named expression over input_names.

Parameters:
  • expr (str)

  • input_names (Sequence[str])

Return type:

str

cana.function_outputs.expression.reorder_positional(expr, old_labels, new_labels)[source]

Rewrite positional x_i indices when input order changes.

old_labels / new_labels identify the same inputs in old vs new bit order (any hashable labels). Token x{i} referred to old_labels[i]; after reorder it must refer to the same label at its new index.

Parameters:
  • expr (str)

  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

str

cana.function_outputs.expression.positional_expression_to_bit_lut(expr, k)[source]

Evaluate positional x0…x{k-1} IR to a dense BitLut.

Refuses when k exceeds MAX_DENSE_LUT_K.

Parameters:
  • expr (str)

  • k (int)

Return type:

BitLut

cana.function_outputs.expression.named_expression_to_bit_lut(expr, input_names)[source]

Evaluate a named boolean expression to a dense BitLut.

Rewrites expr to positional IR via named_to_positional(), then evaluates with positional_expression_to_bit_lut().

Refuses when len(input_names) exceeds MAX_DENSE_LUT_K.

Parameters:
  • expr (str)

  • input_names (Sequence[str])

Return type:

BitLut

cana.function_outputs.expression.bit_lut_to_expression(input_names, lut, style='python')[source]

Build a DNF expression for a BitLut using input_names.

Positional vs named is chosen by input_names: pass positional_names() (k) for positional IR, or input names for a named expression. Callers with a plain 0/1 sequence should convert via BitLut.from_sequence() first.

Parameters:
  • input_names (Sequence[str]) – Input variable names in LUT bit order (MSB first).

  • lut (BitLut) – Dense truth table (lut.k must equal len(input_names)).

  • style (str) – "python" (and/or/not) or "symbolic" (&/|/!).

Return type:

str

cana.function_outputs.expression.boolean_function_to_named_expression(outputs, input_names, style='python', *, node_label=None, warn=True)[source]

Build a named expression from BooleanFunction peers.

Priority: stored positional expression → two-symbol → one-symbol → LUT.

Never runs verify_*; only reads representations_consistent / one_symbol_is_prime_implicant / two_symbol_is_canonical. Soft-warns (never raises) for the chosen non-LUT source: one-symbol when one_symbol_is_prime_implicant is not True; two-symbol when two_symbol_is_canonical is not True; consistency only when representations_consistent is False (None is silent — common after lazy LUT fill).

Parameters:
  • outputs (Any) – A BooleanFunction (duck-typed).

  • input_names (Sequence[str]) – Input names in LUT / expression bit order.

  • style (str) – "python" or "symbolic".

  • node_label (str | None) – Optional node name for warning text.

  • warn (bool) – If False, suppress verification soft-warnings.

Return type:

str