cnf.py
cnf.py
Constructors and functions for sentences in conjunctive normal form (Cnf).
Clause
Bases: frozenset[Lit]
Clause
is a subclass of frozenset[Lit]
.
Source code in normal_form/cnf.py
58 59 60 61 62 63 |
|
Cnf
Bases: frozenset[Clause]
Cnf
is a subclass of frozenset[Clause]
.
Source code in normal_form/cnf.py
66 67 68 69 70 71 72 |
|
__str__()
Pretty print a Cnf after sorting its sorted clause tuples.
Source code in normal_form/cnf.py
69 70 71 72 |
|
absolute_value(literal)
Unnegated form of a Lit.
This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
literal |
obj: |
required |
Return
Check that literal
is not of type Bool and then return the absolute value of
literal
. If it is of type Bool, then return literal
as is.
Source code in normal_form/cnf.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
assign(cnf_instance, assignment)
Assign Bool values to Variables if present in Cnf.
For each Variable (key) in assignment
, replace the Variable and its negation in
cnf_instance
using the Bool (value) in assignment
. The final output is always
tautologically reduced. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnf_instance |
obj: |
required | |
assignment |
obj: |
required |
Edge case
An empty assignment dict results in cnf_instance
simply getting tautologically
reduced.
Return
Tautologically-reduced Cnf formed by replacing every key in the assignment
dict (and
those keys' negations) by corresponding Bool values.
Source code in normal_form/cnf.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
assign_variable_in_clause(clause_instance, variable_instance, boolean)
Assign Bool value to a Variable if present in Clause.
Replace all instances of variable_instance
and its negation in clause_instance
with
boolean
and its negation respectively. Leave all else unchanged. Perform tautological
reductions on the Clause before returning results. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clause_instance |
obj: |
required | |
variable_instance |
obj: |
required | |
boolean |
obj: |
required |
Return
Tautologically-reduced Clause formed by assigning variable_instance
to boolean
in clause_instance
.
Source code in normal_form/cnf.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
assign_variable_in_cnf(cnf_instance, variable_instance, boolean)
Assign Bool value to a Variable if present in Cnf.
Replace all instances of variable_instance
and its negation in cnf_instance
with
boolean
and its negation respectively. Leave all else unchanged. Perform tautological
reductions on the Cnf before returning results. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnf_instance |
obj: |
required | |
variable_instance |
obj: |
required | |
boolean |
obj: |
required |
Return
Tautologically-reduced Cnf formed by assigning variable_instance
to boolean
in
cnf_instance
.
Source code in normal_form/cnf.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
assign_variable_in_lit(literal, variable_instance, boolean)
Assign Bool value to a Variable if present in Lit.
Replace all instances of variable_instance
and its negation with boolean
and its
negation respectively. Leave all else unchanged. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
literal |
obj: |
required | |
variable_instance |
obj: |
required | |
boolean |
obj: |
required |
Return
Lit formed by assigning variable_instance
to boolean
in literal
.
Source code in normal_form/cnf.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
clause(lit_iterable)
Constructor-function for Clause type.
By definition, a Clause
is a nonempty frozenset of Lits. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_iterable |
obj: |
required |
Return
Check that each element in the iterable satisfies axioms for being a Lit and then cast to Clause.
Raises:
Type | Description |
---|---|
ValueError
|
if |
Source code in normal_form/cnf.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
cnf(clause_collection)
Constructor-function for Cnf type.
By definition, a Cnf
is a nonempty frozenset of Clauses. This function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clause_collection |
obj: |
required |
Return
Check that each element in the iterable satisfies axioms for being a Clause and then cast to Cnf.
Raises:
Type | Description |
---|---|
ValueError
|
if |
Source code in normal_form/cnf.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
lits(cnf_instance)
Return frozenset of all Lits that appear in a Cnf.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnf_instance |
obj: |
required |
Return
A frozenset of all lits that appear in a Cnf.
Source code in normal_form/cnf.py
206 207 208 209 210 211 212 213 214 215 |
|
neg(literal)
Negate a Lit.
This function is an involution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
literal |
obj: |
required |
Return
Return the Lit cast from the negative of literal
. If literal
is of type
Bool, then return TRUE
for FALSE
, FALSE
for TRUE
.
Source code in normal_form/cnf.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
tautologically_reduce_clause(clause_instance)
Reduce a Clause using various tautologies.
The order in which these reductions are performed is important. This function is idempotent.
Tautologies affecting Clauses
(⊤ ∨ c = ⊤) (⊥ = ⊥) (⊥ ∨ c = c) (c ∨ ¬c = ⊤),
where x
is a Clause, ⊤
represents TRUE
, ⊥
represents FALSE
, and ∨
is
disjunction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clause_instance |
obj: |
required |
Return
The Clause formed by performing all the above-mentioned tautological reductions.
Source code in normal_form/cnf.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
tautologically_reduce_cnf(cnf_instance)
Reduce a Cnf using various tautologies.
The order in which these reductions are performed is important. This function is idempotent. This is a recursive function that is guaranteed to terminate.
Tautologies affecting Cnfs
(x ∧ ⊥ = ⊥) (⊤ = ⊤) (⊤ ∧ x = x),
where x
is a Cnf, ⊤
represents TRUE
, ⊥
represents FALSE
, and ∧
is
conjunction.
There is also a special reduction for Cnfs
(x ∧ y = x IF x < y),
where x
is a Cnf, y
is another Cnf, and x < y
means that every clause of x
is also present as a clause in y
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnf_instance |
obj: |
required |
Return
The Cnf formed by first reducing all the clauses tautologically and then performing all the above-mentioned tautological reductions on the Cnf itself.
Source code in normal_form/cnf.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
variable(positive_int)
Constructor-function for Variable type.
By definition, a Variable
is just a positive integer. This
function is idempotent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
positive_int |
obj: |
required |
Return
If input is indeed positive, then return positive_int
after casting to Variable.
Raises:
Type | Description |
---|---|
ValueError
|
if |
Source code in normal_form/cnf.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|