cana.function_outputs package

Multi-representation Boolean functions for a node.

BooleanFunction is what cana.boolean_node.BooleanNode.function stores. Peer representation slots may hold a dense BitLut, one-symbol (prime-implicant) schemata, two-symbol schemata, and/or a positional expression. Multiple representations may coexist; named converters document each derivation, and lazy getters (get_bit_lut(), get_one_symbol_schemata(), get_two_symbol_schemata(), get_positional_expression()) try every peer slot, cache intermediates computed along the way, and raise ValueError when none can produce the requested representation.

Dense Sequence access (len, [i], iterate) and eval() prefer bit_lut and materialize it from one-symbol schemata, two-symbol schemata (via one-symbol), or a positional expression via get_bit_lut() when needed.

class cana.function_outputs.BitLut(k=0, *, fill=0)[source]

Bases: object

Dense Boolean truth table packed 8 entries per byte.

Row i (input state number, matching statenum_to_binstate()) lives in bit (i & 7) of byte i >> 3 (LSB-first within each byte). Indexing and iteration yield int values in {0, 1}.

Parameters:
  • k (int)

  • fill (int)

classmethod from_sequence(seq)[source]

Build a BitLut from any 0/1 sequence of length 2**k.

Parameters:

seq (Sequence[object])

Return type:

BitLut

classmethod from_bytes(data, k)[source]

Build from packed bytes with the same layout as to_bytes().

Parameters:
  • data (bytes | bytearray)

  • k (int)

Return type:

BitLut

property k: int

Number of inputs (len(self) == 2**k, or 1 when k == 0).

eval(statenum)[source]

Return the output for input state number statenum.

Parameters:

statenum (int)

Return type:

int

to_list()[source]

Return an unpacked list of 0/1 ints.

Return type:

List[int]

to_bytes()[source]

Return a copy of the packed byte buffer.

Return type:

bytes

fill(value)[source]

Set every row to 0 or 1.

Parameters:

value (object)

Return type:

None

is_constant()[source]

Return 0 or 1 if every row equals that value, else None.

Return type:

int | None

popcount()[source]

Number of rows equal to 1.

Return type:

int

negate_input(idx)[source]

Return a copy with input bit idx negated (MSB-first bit order).

Row i of the result equals row i ^ (1 << (k - 1 - idx)) of self, matching the legacy negate_LUT_input remapping.

Parameters:

idx (int)

Return type:

BitLut

lut_as_str()[source]

Return a multi-line string of binstate : output rows.

Return type:

str

reorder(old_labels, new_labels)[source]

Return a copy with input-bit order remapped from old_labels to new_labels.

Labels identify the k input variables in MSB-first bit order. new_labels must be a permutation of old_labels. If the orders already match, returns self unchanged.

Parameters:
  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

BitLut

classmethod reorder_sequence(lut, old_labels, new_labels)[source]

Like reorder(), but accepts any 0/1 sequence or BitLut.

Parameters:
  • lut (Sequence[object])

  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

BitLut

cana.function_outputs.refuse_dense_lut_k(k, *, via)[source]

Raise ValueError when materializing a dense BitLut would be too large.

Parameters:
  • k (int)

  • via (str)

Return type:

None

class cana.function_outputs.BooleanFunction(bit_lut=None, one_symbol_schemata=None, two_symbol_schemata=None, positional_expression=None, k=None)[source]

Bases: object

One Boolean update rule under one or more peer representations.

Parameters:
  • bit_lut (Optional[BitLut])

  • one_symbol_schemata (Optional[OneSymbolSchemata])

  • two_symbol_schemata (Optional[TwoSymbolSchemata])

  • positional_expression (Optional[Any])

  • k (Optional[int])

bit_lut

Dense packed truth table of length 2**k, if known.

one_symbol_schemata

Prime-implicant / one-symbol (wildcard) cover: dict keyed by polarity 0/1 with sets of length-k strings over {0,1,2} (2 = don’t-care), if known.

two_symbol_schemata

Two-symbol schemata as (ts0, ts1), if known. Each list holds [schema_str, perm_groups, same_groups] entries.

positional_expression

Positional python IR (and/or/not over x0…x{k-1}), if known. Network names are rebound at the BooleanNode / I/O boundary.

one_symbol_is_prime_implicant

None until known; True/False after QM compute or verify_one_symbol_schemata().

two_symbol_is_canonical

None until known; True/False after deriving two-symbol from a verified PI cover or verify_two_symbol_schemata().

representations_consistent

None until known; True/False after verify_representations_consistent().

_pi_coverage / _ts_coverage / _sampled_pi_coverage

Optional analysis caches owned by the node API; cleared automatically when one_symbol_schemata or two_symbol_schemata is replaced with a non-equal value.

bit_lut
positional_expression
one_symbol_is_prime_implicant: bool | None
two_symbol_is_canonical: bool | None
representations_consistent: bool | None
classmethod from_sequence(seq)[source]

Build from a dense 0/1 sequence (packs into a BitLut).

Parameters:

seq (Sequence[object])

Return type:

BooleanFunction

property k: int

Number of inputs inferred from the dense LUT or set explicitly.

property one_symbol_schemata: Dict[int, Set[str]] | None
property two_symbol_schemata: Tuple[list, list] | None
eval(statenum)[source]

Evaluate the function at input state statenum.

Prefers bit_lut. If absent, materializes via get_bit_lut() from one-symbol schemata, two-symbol schemata (via one-symbol), or a positional expression.

Parameters:

statenum (int)

Return type:

int

to_list()[source]

Unpack the dense LUT to a list of ints.

Return type:

List[int]

is_constant()[source]

Return 0/1 if the function is constant, else None.

Uses bit_lut when present; otherwise recognizes positional True/False expressions with k=0.

Return type:

int | None

get_bit_lut()[source]

Return the dense LUT, building from schemata or expression if needed.

Tries stored bit_lut, then one-symbol, then positional_expression, then two-symbol (expand to one-symbol, then build the LUT). Intermediates are cached. Raises ValueError if no peer can produce a LUT.

Return type:

BitLut

get_one_symbol_schemata()[source]

Return one-symbol schemata, deriving from any available peer slot.

Preference order:

  1. stored one_symbol_schemata (soft-warns unless one_symbol_is_prime_implicant is True)

  2. expand two_symbol_schemata (does not set one_symbol_is_prime_implicant; no soft-warn on first expand)

  3. expression cascade (syntactic ROF, then Primer-B) when positional_expression is set — stores schemata only, not a LUT

  4. Quine–McCluskey from stored bit_lut, or build+store bit_lut from positional_expression (subject to MAX_DENSE_LUT_K), then QM

Intermediates are cached. Raises ValueError if no peer can produce one-symbol schemata.

Return type:

Dict[int, Set[str]]

get_two_symbol_schemata()[source]

Return two-symbol schemata, deriving from any available peer slot.

Prefer stored two-symbol (soft-warns unless two_symbol_is_canonical is True); else derive from stored one_symbol_schemata without going through get_one_symbol_schemata() (soft-warns unless the derived cover is marked canonical, i.e. when the one-symbol was a verified PI cover); else obtain primes via get_one_symbol_schemata() (expression cascade, then bit_lut/QM) and derive. Intermediates are cached. Does not re-run verification; call verify_two_symbol_schemata() to check explicitly. Raises ValueError if no peer can produce two-symbol schemata.

Return type:

Tuple[list, list]

get_positional_expression()[source]

Return positional expression IR, deriving from any available peer.

Prefer stored positional_expression; else build from two_symbol_schemata, then one_symbol_schemata, then bit_lut (positional x0… x{k-1} python IR). Does not call get_bit_lut() so an empty container fails here rather than via a side-effect materialization. Raises ValueError if no peer can produce an expression.

Named / styled export stays in boolean_function_to_named_expression().

Return type:

Any

build_bit_lut_from_positional_expression()[source]

Build and store bit_lut from positional_expression.

Return type:

BitLut

build_bit_lut_from_one_symbol_schemata()[source]

Build and store bit_lut from one_symbol_schemata.

Return type:

BitLut

static one_symbol_to_bit_lut(schemata, k)

Build a BitLut from one-symbol schemata.

Raises:

ValueError – If k exceeds MAX_DENSE_LUT_K, or on polarity conflicts or incomplete coverage of the 2**k rows. Incomplete covers are not filled with a default output value.

Parameters:
  • schemata (Dict[int, Set[str]])

  • k (int)

Return type:

BitLut

compute_one_symbol_schemata_from_bit_lut()[source]

Compute and store one-symbol schemata from bit_lut via Quine-McCluskey.

Return type:

Dict[int, Set[str]]

verify_one_symbol_schemata()[source]

Opt-in: stored one-symbol schemata are complete, consistent, and PIs.

Builds a LUT from the stored schemata (fails on conflict/incompleteness), re-runs Quine-McCluskey, and checks set equality with the stored cover. Sets one_symbol_is_prime_implicant. Never called by normal canalization paths.

Return type:

bool

verify_two_symbol_schemata()[source]

Opt-in: stored two-symbol is the canonical find_two_symbols_v2 of PIs.

Expands two → one. If a verified one-symbol cover is already stored: mismatch is a quick False; match skips the PI check and rebuilds from that cover. Otherwise PI-checks the expansion (LUT + QM). Then rebuilds two-symbol locally and requires nested equality with the stored value (including same_groups). Sets two_symbol_is_canonical. Never called by normal canalization paths.

Return type:

bool

verify_representations_consistent()[source]

Opt-in: all convertible representations describe the same function.

Derives a LUT from each populated slot that can produce one (stored bit_lut, one_symbol_schemata, two_symbol_schemata, and positional positional_expression) and checks they agree. Sets representations_consistent. Never called by normal canalization paths.

Return type:

bool

reorder(old_labels, new_labels)[source]

Return a copy with input order remapped across filled peer reps.

Remaps bit_lut, one_symbol_schemata, two_symbol_schemata, and positional positional_expression when present (no QM). Preserves one_symbol_is_prime_implicant and two_symbol_is_canonical. If orders already match, returns self unchanged.

Parameters:
  • old_labels (Sequence[object])

  • new_labels (Sequence[object])

Return type:

BooleanFunction

cana.function_outputs.two_symbol_to_one_symbol(ts)[source]

Expand two-symbol schemata to one-symbol (F’) covers.

For each polarity and each [schema, perm_groups, _], permute values within each perm group and collect length-k strings over {0,1,2}. Does not re-run Quine–McCluskey; the result is the redescribed F’ cover, not necessarily flagged as prime-implicant.

Parameters:

ts (Tuple[list, list])

Return type:

Dict[int, Set[str]]

cana.function_outputs.two_symbol_to_expression(input_names, ts, style='python')[source]

Build a DNF-like expression from two-symbol ON schemata (polarity 1).

Each schema becomes an AND of fixed literals and perm-group subexpressions (each group is an OR of the distinct permutation clauses). Schemas are OR’d together. Schema characters: 1 positive, 0 negated, 2 don’t-care. same_groups are ignored.

Parameters:
  • input_names (Sequence[str])

  • ts (Tuple[list, list])

  • style (str)

Return type:

str

Submodules