cana.primes package

Expression → prime implicants: syntactic ROF, Primer-B, and cascade.

LUT / Quine–McCluskey remain on BooleanFunction and are tried after the expression cascade when deriving one-symbol schemata.

Wall-clock timeouts are not part of this package; see dev/high_k.timing.

class cana.primes.LogicNode(op, children)[source]

Bases: object

Node in a boolean expression AST.

op is one of AND, OR, LITERAL, NEGATED_LITERAL. For leaves, children is the variable name (str); for gates it is a list of child LogicNode instances.

Parameters:
  • op (str)

  • children (Union[str, List['LogicNode']])

op
children
cana.primes.ast_leaf_polarities(node)[source]

Collect (name, polarity) for each leaf (preorder, with multiplicity).

LITERAL → (name, "pos"); NEGATED_LITERAL → (name, "neg").

Parameters:

node (LogicNode | None)

Return type:

List[Tuple[str, Literal[‘pos’, ‘neg’]]]

cana.primes.ast_variable_names(node)[source]

Collect leaf variable names in node (preorder, with multiplicity).

Parameters:

node (LogicNode | None)

Return type:

List[str]

cana.primes.invert_ast(node)[source]

Apply De Morgan dualization (swap AND/OR, flip literals).

Parameters:

node (LogicNode)

Return type:

LogicNode

cana.primes.expression_to_ast(expr_str)[source]

Parse a boolean expression into a LogicNode AST.

Accepts python (and/or/not) or symbolic (&/|/!) operators. Splits on top-level | then &, unwraps outer parentheses, and treats ! as a prefix pushed via invert_ast() (NNF; stacked !!a cancels).

Parameters:

expr_str (str)

Return type:

LogicNode | None

class cana.primes.PrimesResult(status, method=None, on_primes=None, off_primes=None, n_on=None, n_off=None, message='')[source]

Bases: object

Outcome of on_off_primes_from_expression().

Parameters:
  • status (Literal['ok', 'too_many', 'refused', 'error', 'unsuccessful'])

  • method (Literal['rof', 'primer'] | None)

  • on_primes (List[Dict[str, bool]] | None)

  • off_primes (List[Dict[str, bool]] | None)

  • n_on (int | None)

  • n_off (int | None)

  • message (str)

status: Literal['ok', 'too_many', 'refused', 'error', 'unsuccessful']
method: Literal['rof', 'primer'] | None = None
on_primes: List[Dict[str, bool]] | None = None
off_primes: List[Dict[str, bool]] | None = None
n_on: int | None = None
n_off: int | None = None
message: str = ''
property ok: bool
as_on_off()[source]
Return type:

Tuple[List[Dict[str, bool]], List[Dict[str, bool]]]

cana.primes.on_off_primes_from_expression(expr, *, max_primes=1000000)[source]

Try syntactic ROF, then Primer-B; never builds a LUT or runs QM.

Parameters:
  • expr (str) – Boolean expression (python or symbolic operators).

  • max_primes (int) – Cap on ON+OFF (ROF) or per-polarity (Primer) prime counts.

Returns:

Soft guardrail hits use status rather than raising, so callers can fall through to a LUT/QM path.

Return type:

PrimesResult

cana.primes.primer_on_off_primes(expr, *, max_primes=1000000)[source]

Exact ON/OFF primes via PySAT Primer-B on f and ~f.

Builds a non-clausal pysat.formula tree, enumerates all prime implicants with Primer (implicates=False), and maps clausification IDs back to variable names. If either polarity would exceed max_primes, enumeration stops and ValueError is raised (no partial result).

Runs in-process with no wall-clock limit; wrap externally if needed.

Raises:
  • ImportError – if pysat (python-sat) is not installed.

  • ValueError – if ON or OFF prime count exceeds max_primes.

  • RecursionError – if parenthesis nesting exceeds MAX_PRIMER_PAREN_DEPTH.

Parameters:
  • expr (str)

  • max_primes (int)

Return type:

Tuple[List[Dict[str, bool]], List[Dict[str, bool]]]

class cana.primes.SafeNetworkPrimes(bnet_content_or_path)[source]

Bases: object

Parse a .bnet string/path and expose per-node PI counts / generators.

Count ON/OFF primes eagerly; expose lazy generators so callers can skip huge enumerations.

Each update rule must be a syntactic ROF; enforced by count_rof_primes() / generate_rof_primes().

Parameters:

bnet_content_or_path (Union[str, Path])

get_on_generator(var_name)[source]
Parameters:

var_name (str)

Return type:

Iterator[Dict[str, bool]] | None

get_off_generator(var_name)[source]
Parameters:

var_name (str)

Return type:

Iterator[Dict[str, bool]] | None

summary()[source]
Return type:

None

cana.primes.check_syntactic_rof(expr)[source]

Return syntactic-ROF stats for expression expr.

is_rof means syntactic (single occurrence), not semantic read-once after simplification. Constants (no identifiers) are vacuously ROFs.

Parameters:

expr (str)

Return type:

Dict[str, bool | int]

cana.primes.check_syntactic_rof_ast(node)[source]

Return syntactic-ROF stats for an AST node (leaf occurrence walk).

Parameters:

node (LogicNode | None)

Return type:

Dict[str, bool | int]

cana.primes.count_on_off_primes(expr)[source]

Return (n_on_primes, n_off_primes) for expression expr.

expr must be a syntactic ROF; asserted via count_rof_primes().

Parameters:

expr (str)

Return type:

Tuple[int, int]

cana.primes.count_rof_primes(node)[source]

Count primes of a syntactic-ROF formula (no enumeration).

Asserts syntactic ROF. Formulas that are only semantic ROF after simplification are rejected. The AST is only the evaluation vehicle.

Parameters:

node (LogicNode | None)

Return type:

int

cana.primes.generate_rof_primes(node)[source]

Yield ON-set (or dualized OFF-set) primes as {var: True/False}.

Asserts syntactic ROF (AND merge via dict.update assumes disjoint variable sets). Formulas that are only semantic ROF after simplification are rejected. The AST is only the evaluation vehicle.

AND children are combined recursively without materializing full child lists via itertools.product.

Parameters:

node (LogicNode | None)

Return type:

Iterator[Dict[str, bool]]

cana.primes.iter_on_off_primes(expr, polarity='on')[source]

Yield prime implicants for the ON or OFF set of expr.

expr must be a syntactic ROF; asserted via generate_rof_primes().

Parameters:
  • expr (str)

  • polarity (Literal['on', 'off'])

Return type:

Iterator[Dict[str, bool]]

cana.primes.require_syntactic_rof_ast(node)[source]

Raise if node is not a syntactic ROF (single occurrence).

Rejects formulas that might only be read-once after simplification; this engine never rewrites.

Parameters:

node (LogicNode | None)

Return type:

None

cana.primes.expr_to_pysat_formula(expr)[source]

Parse a symbolic & / | / ! expression into a pysat Formula.

Precedence: ! > & > |; parentheses override. Identifiers are [A-Za-z_][A-Za-z0-9_]*. Constants 0 / 1 / True / False are accepted. Raises ImportError if pysat is missing, ValueError on syntax errors.

Parameters:

expr (str)

cana.primes.one_symbol_to_primes(schemata, input_names)[source]

Convert one-symbol schemata to named ON/OFF prime-cube lists.

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

  • input_names (Sequence[str])

Return type:

Tuple[List[Dict[str, bool]], List[Dict[str, bool]]]

cana.primes.paren_nesting_depth(expr)[source]

Return the maximum () nesting depth in expr.

Parameters:

expr (str)

Return type:

int

cana.primes.primes_to_one_symbol(on_primes, off_primes, *, input_names=None, k=None)[source]

Convert named ON/OFF prime cubes to positional one-symbol schemata.

input_names defaults to positional_names(k). When k is omitted it is inferred from input_names, else from the union of prime keys (sorted).

Parameters:
  • on_primes (List[Dict[str, bool]])

  • off_primes (List[Dict[str, bool]])

  • input_names (Sequence[str] | None)

  • k (int | None)

Return type:

Dict[int, Set[str]]

cana.primes.refuse_extreme_nesting(expr, *, limit=None)[source]

Raise RecursionError if parenthesis nesting exceeds limit.

Default limit is min(MAX_SAFE_PAREN_DEPTH, getrecursionlimit() - 50). Pass a backend-specific cap (e.g. MAX_PRIMER_PAREN_DEPTH) when the parser’s stack cost is known.

Parameters:
  • expr (str)

  • limit (int | None)

Return type:

None

Submodules