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:
objectDense Boolean truth table packed 8 entries per byte.
Row
i(input state number, matchingstatenum_to_binstate()) lives in bit(i & 7)of bytei >> 3(LSB-first within each byte). Indexing and iteration yieldintvalues in{0, 1}.- Parameters:
k (int)
fill (int)
- classmethod from_sequence(seq)[source]
Build a
BitLutfrom any 0/1 sequence of length2**k.- Parameters:
seq (Sequence[object])
- Return type:
- 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:
- property k: int
Number of inputs (
len(self) == 2**k, or 1 whenk == 0).
- eval(statenum)[source]
Return the output for input state number
statenum.- Parameters:
statenum (int)
- Return type:
int
- is_constant()[source]
Return
0or1if every row equals that value, elseNone.- Return type:
int | None
- negate_input(idx)[source]
Return a copy with input bit
idxnegated (MSB-first bit order).Row
iof the result equals rowi ^ (1 << (k - 1 - idx))ofself, matching the legacynegate_LUT_inputremapping.- Parameters:
idx (int)
- Return type:
- reorder(old_labels, new_labels)[source]
Return a copy with input-bit order remapped from
old_labelstonew_labels.Labels identify the
kinput variables in MSB-first bit order.new_labelsmust be a permutation ofold_labels. If the orders already match, returnsselfunchanged.- Parameters:
old_labels (Sequence[object])
new_labels (Sequence[object])
- Return type:
- cana.function_outputs.refuse_dense_lut_k(k, *, via)[source]
Raise
ValueErrorwhen 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:
objectOne 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:
dictkeyed by polarity0/1with sets of length-kstrings 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/notoverx0…x{k-1}), if known. Network names are rebound at theBooleanNode/ I/O boundary.
- one_symbol_is_prime_implicant
Noneuntil known;True/Falseafter QM compute orverify_one_symbol_schemata().
- two_symbol_is_canonical
Noneuntil known;True/Falseafter deriving two-symbol from a verified PI cover orverify_two_symbol_schemata().
- representations_consistent
Noneuntil known;True/Falseafterverify_representations_consistent().
- _pi_coverage / _ts_coverage / _sampled_pi_coverage
Optional analysis caches owned by the node API; cleared automatically when
one_symbol_schemataortwo_symbol_schematais 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:
- 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 viaget_bit_lut()from one-symbol schemata, two-symbol schemata (via one-symbol), or a positional expression.- Parameters:
statenum (int)
- Return type:
int
- is_constant()[source]
Return
0/1if the function is constant, elseNone.Uses
bit_lutwhen present; otherwise recognizes positionalTrue/Falseexpressions withk=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. RaisesValueErrorif no peer can produce a LUT.- Return type:
- get_one_symbol_schemata()[source]
Return one-symbol schemata, deriving from any available peer slot.
Preference order:
stored
one_symbol_schemata(soft-warns unlessone_symbol_is_prime_implicantisTrue)expand
two_symbol_schemata(does not setone_symbol_is_prime_implicant; no soft-warn on first expand)expression cascade (syntactic ROF, then Primer-B) when
positional_expressionis set — stores schemata only, not a LUTQuine–McCluskey from stored
bit_lut, or build+storebit_lutfrompositional_expression(subject toMAX_DENSE_LUT_K), then QM
Intermediates are cached. Raises
ValueErrorif 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_canonicalisTrue); else derive from storedone_symbol_schematawithout going throughget_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 viaget_one_symbol_schemata()(expression cascade, then bit_lut/QM) and derive. Intermediates are cached. Does not re-run verification; callverify_two_symbol_schemata()to check explicitly. RaisesValueErrorif 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 fromtwo_symbol_schemata, thenone_symbol_schemata, thenbit_lut(positionalx0…x{k-1}python IR). Does not callget_bit_lut()so an empty container fails here rather than via a side-effect materialization. RaisesValueErrorif 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_lutfrompositional_expression.- Return type:
- build_bit_lut_from_one_symbol_schemata()[source]
Build and store
bit_lutfromone_symbol_schemata.- Return type:
- static one_symbol_to_bit_lut(schemata, k)
Build a
BitLutfrom one-symbol schemata.- Raises:
ValueError – If
kexceedsMAX_DENSE_LUT_K, or on polarity conflicts or incomplete coverage of the2**krows. Incomplete covers are not filled with a default output value.- Parameters:
schemata (Dict[int, Set[str]])
k (int)
- Return type:
- compute_one_symbol_schemata_from_bit_lut()[source]
Compute and store one-symbol schemata from
bit_lutvia 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_v2of 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 (includingsame_groups). Setstwo_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 positionalpositional_expression) and checks they agree. Setsrepresentations_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 positionalpositional_expressionwhen present (no QM). Preservesone_symbol_is_prime_implicantandtwo_symbol_is_canonical. If orders already match, returnsselfunchanged.- Parameters:
old_labels (Sequence[object])
new_labels (Sequence[object])
- Return type:
- 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-kstrings 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:
1positive,0negated,2don’t-care.same_groupsare ignored.- Parameters:
input_names (Sequence[str])
ts (Tuple[list, list])
style (str)
- Return type:
str