sweetpea.core.cnf module¶
This module provides the core data types used in SweetPea Core.
Fundamentally, SweetPea deals with Boolean logic formulas written in conjunctive normal form (CNF). Wikipedia says:
a formula is in conjunctive normal form (CNF) […] if it is a conjunction of one or more clauses, where a clause is a disjunction of literals; otherwise put, it is a product of sums or an AND of ORs.
In SweetPea, we refer to the “literals” as Vars
. These are
combined into Clauses
, which are then combined into
CNFs
. Most SweetPea functionality revolves around building and
manipulating CNFs
.
- class sweetpea.core.cnf.Var(value)¶
Bases:
object
A variable for use in a CNF formula.
This is essentially a wrapper for the builtin
int
type, but with a few advantages:It is semantically distinct from an
int
, so reading code is more sensible.Type aliases for
int
can be obnoxious to deal with because you may want to useint
-supported operations (like addition, negation, etc), but these are not cross-type compatible (e.g.,Var(3) + 2
produces an error in mypy).We can implement custom behavior as a sort of micro-DSL without much overhead or issue.
- Parameters
value (int) –
- property value: int¶
The integer value of this variable.
- class sweetpea.core.cnf.Clause(first_value=None, *rest_values)¶
Bases:
sweetpea.core.simple_sequence.SimpleSequence
[sweetpea.core.cnf.Var
]A sequence of
Vars
. AClause
indicates a logical disjunction between the contained variables. For example,Clause(Var(3), Var(7))
encodes (3 ∨ 7).A
Clause
can also be instantiated with alist
ofVars
. This mode of instantiation will also accept rawints
in addition to instances ofVar
. For example,Clause([1, -2, 3])
corresponds to the formula (1 ∨ ¬2 ∨ 3).
- class sweetpea.core.cnf.CNF(*values)¶
Bases:
sweetpea.core.simple_sequence.SimpleSequence
[sweetpea.core.cnf.Clause
]A conjunction of disjunction
Clauses
. For example,CNF(Clause(Var(3), Var(7)), Clause(Var(1), Var(13)))
corresponds to the CNF formula ((3 ∨ 7) ∧ (1 ∨ 13)).CNFs
can also be instantiated with alist
oflists
ofVars
, where each innerlist
represents aClause
and the outerlist
represents the CNF formula itself. Similar toClause
, this mode of instantiation will also accept rawints
in addition to instances ofVar
. For example,CNF([[1, 2, -3], [-2, 7, 1]])
corresponds to the CNF formula ((1 ∨ 2 ∨ ¬3) ∧ (¬2 ∨ 7 ∨ 1)).- static from_fresh(fresh)¶
Returns an empty
CNF
with the given number of freshVars
already allocated.Note
This method probably shouldn’t exist! The number of fresh variables should be deduced from the formulas themselves. This exists for legacy compatibility and should eventually be removed.
- Parameters
fresh (int) –
- Return type
- static and_vars(a, b)¶
Returns a CNF formula encoding (a ∧ b).
- Parameters
a (Union[int, sweetpea.core.cnf.Var]) –
b (Union[int, sweetpea.core.cnf.Var]) –
- Return type
- static or_vars(a, b)¶
Returns a CNF formula encoding (a ∨ b).
- Parameters
a (Union[int, sweetpea.core.cnf.Var]) –
b (Union[int, sweetpea.core.cnf.Var]) –
- Return type
- static xor_vars(a, b)¶
Returns a CNF formula encoding (a ⊕ b) as ((a ∨ b) ∧ (¬a ∨ ¬b)).
- Parameters
a (Union[int, sweetpea.core.cnf.Var]) –
b (Union[int, sweetpea.core.cnf.Var]) –
- Return type
- static xnor_vars(a, b)¶
Returns a CNF formula encoding (a ⊙ b) as ((a ∨ ¬b) ∧ (¬a ∨ b)).
Note
(a ⊙ b) is logically equivalent to (a ⇔ b), so this function replaces the original Haskell version’s
doubleImplies
function.- Parameters
a (Union[int, sweetpea.core.cnf.Var]) –
b (Union[int, sweetpea.core.cnf.Var]) –
- Return type
- static distribute(v, cnf)¶
Distributes the given
Var
across eachClause
of the given CNF formula, producing a new :class`CNF`.- Parameters
v (sweetpea.core.cnf.Var) –
cnf (sweetpea.core.cnf.CNF) –
- Return type
- as_dimacs_string(fresh_variable_count=None)¶
Represents the
CNF
as a string in the DIMACS format.The DIMACS format is a standardized method of representing CNF formulas as strings. This implementation is based on the details given here.
- as_unigen_string(fresh_variable_count=None, support_set_length=None, sampled_variables=None)¶
Returns a string representing the CNF formula in the modified DIMACS format used by Unigen.
See
CNF.as_dimacs_string()
for details about the DIMACS format.Unigen adds an extra line at the top that describes the list of variables you would like to sample (instead of sampling among all variables). This line is given in the form:
c ind v1 v2 ... vn 0
where
v1
,v2
, …,vn
represent variables in the formula. This line is placed just below the “problem” line (the line beginning withp
).- Parameters
fresh_variable_count (Optional[int]) –
support_set_length (Optional[int]) –
sampled_variables (Optional[List[sweetpea.core.cnf.Var]]) –
- Return type
- as_haskell_cnf()¶
Converts the
CNF
to atuple
whose first element is the number of fresh variables in the formula and whose second element is theCNF
represented as alist
oflists
ofints
.Note
This is the way CNF formulas were encoded in the original Haskell code. It only exists for consistency checks across the two code bases.
- get_n_fresh(n)¶
Creates the next n
Vars
, numbered sequentially.- Parameters
n (int) –
- Return type
List[sweetpea.core.cnf.Var]
- append(other)¶
-
- Parameters
other (Union[sweetpea.core.cnf.CNF, sweetpea.core.cnf.Clause, Iterable[sweetpea.core.cnf.Clause], sweetpea.core.cnf.Var]) –
- prepend(other)¶
-
- Parameters
other (Union[sweetpea.core.cnf.CNF, sweetpea.core.cnf.Clause, Iterable[sweetpea.core.cnf.Clause], sweetpea.core.cnf.Var]) –
- set_to_zero(variable)¶
Zeroes the specified
Var
by appending its negation to the existing CNF formula.- Parameters
variable (sweetpea.core.cnf.Var) –
- zero_out(in_list)¶
Appends a CNF formula negating the existing CNF formula.
- Parameters
in_list (Iterable[sweetpea.core.cnf.Var]) –
- set_to_one(variable)¶
Sets the specified variable to
1
by appending it to the existing CNF formula.- Parameters
variable (sweetpea.core.cnf.Var) –
- assert_k_of_n(k, in_list)¶
- Parameters
k (int) –
in_list (Sequence[sweetpea.core.cnf.Var]) –
- assert_k_less_than_n(k, in_list)¶
- Parameters
k (int) –
in_list (Sequence[sweetpea.core.cnf.Var]) –
- assert_k_greater_than_n(k, in_list)¶
- Parameters
k (int) –
in_list (Sequence[sweetpea.core.cnf.Var]) –
- pop_count(in_list)¶
Returns the list of
Vars
that represents the bits of the given list variable in binary.- Parameters
in_list (Sequence[sweetpea.core.cnf.Var]) –
- Return type
List[sweetpea.core.cnf.Var]
- half_adder(a, b)¶
- Parameters
a (sweetpea.core.cnf.Var) –
b (sweetpea.core.cnf.Var) –
- Return type
- full_adder(a, b, cin)¶
- Parameters
a (sweetpea.core.cnf.Var) –
b (sweetpea.core.cnf.Var) –
cin (sweetpea.core.cnf.Var) –
- Return type
- ripple_carry(xs, ys)¶
- Parameters
xs (List[sweetpea.core.cnf.Var]) –
ys (List[sweetpea.core.cnf.Var]) –
- Return type
Tuple[List[sweetpea.core.cnf.Var], List[sweetpea.core.cnf.Var]]