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_ior named inputs).
positional_expression_to_bit_lut/named_expression_to_bit_lut— evaluate IR to a dense LUT.bit_lut_to_expression— emit whatever names are ininput_names(passpositional_names()for positional IR).normalize_operators()/expression_identifiers()— either kind.
- cana.function_outputs.expression.normalize_operators(expr, style='python')[source]
Normalize boolean operators to
and/or/notor&/|/!.Works on positional or named expressions (operators only). Prefix
~is treated as logical NOT (same as!/not), not Python bitwise complement.- Parameters:
expr (str) – Boolean expression string.
style (str) –
"python"(forpositional_expression_to_bit_lut()/named_expression_to_bit_lut()) or"symbolic".
- 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_namesorder is preserved. Intended for named (input) expressions, not positionalx_iIR.- 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_iIR to a named expression overinput_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_iindices when input order changes.old_labels/new_labelsidentify the same inputs in old vs new bit order (any hashable labels). Tokenx{i}referred toold_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 denseBitLut.Refuses when
kexceedsMAX_DENSE_LUT_K.- Parameters:
expr (str)
k (int)
- Return type:
- 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 withpositional_expression_to_bit_lut().Refuses when
len(input_names)exceedsMAX_DENSE_LUT_K.- Parameters:
expr (str)
input_names (Sequence[str])
- Return type:
- cana.function_outputs.expression.bit_lut_to_expression(input_names, lut, style='python')[source]
Build a DNF expression for a
BitLutusinginput_names.Positional vs named is chosen by
input_names: passpositional_names()(k)for positional IR, or input names for a named expression. Callers with a plain 0/1 sequence should convert viaBitLut.from_sequence()first.- Parameters:
input_names (Sequence[str]) – Input variable names in LUT bit order (MSB first).
lut (BitLut) – Dense truth table (
lut.kmust equallen(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
BooleanFunctionpeers.Priority: stored positional expression → two-symbol → one-symbol → LUT.
Never runs
verify_*; only readsrepresentations_consistent/one_symbol_is_prime_implicant/two_symbol_is_canonical. Soft-warns (never raises) for the chosen non-LUT source: one-symbol whenone_symbol_is_prime_implicantis notTrue; two-symbol whentwo_symbol_is_canonicalis notTrue; consistency only whenrepresentations_consistent is False(Noneis 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