propositions.semantics module¶
Semantic analysis of propositional-logic constructs.
-
propositions.semantics.
Model
¶ A model for propositional-logic formulas, a mapping from variable names to truth values.
alias of Mapping[str, bool]
-
propositions.semantics.
is_model
(model)¶ Checks if the given dictionary is a model over some set of variable names.
-
propositions.semantics.
variables
(model)¶ Finds all variable names over which the given model is defined.
- Parameters
- Return type
- Returns
A set of all variable names over which the given model is defined.
-
propositions.semantics.
evaluate
(formula, model)¶ Calculates the truth value of the given formula in the given model.
- Parameters
- Return type
- Returns
The truth value of the given formula in the given model.
Examples
>>> evaluate(Formula.parse('~(p&q76)'), {'p': True, 'q76': False}) True
>>> evaluate(Formula.parse('~(p&q76)'), {'p': True, 'q76': True}) False
-
propositions.semantics.
all_models
(variables)¶ Calculates all possible models over the given variable names.
- Parameters
variables (
Sequence
[str
]) – variable names over which to calculate the models.- Return type
- Returns
An iterable over all possible models over the given variable names. The order of the models is lexicographic according to the order of the given variable names, where False precedes True.
Examples
>>> list(all_models(['p', 'q'])) [{'p': False, 'q': False}, {'p': False, 'q': True}, {'p': True, 'q': False}, {'p': True, 'q': True}]
>>> list(all_models(['q', 'p'])) [{'q': False, 'p': False}, {'q': False, 'p': True}, {'q': True, 'p': False}, {'q': True, 'p': True}]
-
propositions.semantics.
truth_values
(formula, models)¶ Calculates the truth value of the given formula in each of the given models.
- Parameters
- Return type
- Returns
An iterable over the respective truth values of the given formula in each of the given models, in the order of the given models.
Examples
>>> list(truth_values(Formula.parse('~(p&q76)'), all_models(['p', 'q76']))) [True, True, True, False]
-
propositions.semantics.
print_truth_table
(formula)¶ Prints the truth table of the given formula, with variable-name columns sorted alphabetically.
- Parameters
formula (
Formula
) – formula to print the truth table of.
Examples
>>> print_truth_table(Formula.parse('~(p&q76)')) | p | q76 | ~(p&q76) | |---|-----|----------| | F | F | T | | F | T | T | | T | F | T | | T | T | F |
- Return type
None
-
propositions.semantics.
is_tautology
(formula)¶ Checks if the given formula is a tautology.
-
propositions.semantics.
is_contradiction
(formula)¶ Checks if the given formula is a contradiction.
-
propositions.semantics.
is_satisfiable
(formula)¶ Checks if the given formula is satisfiable.
-
propositions.semantics.
_synthesize_for_model
(model)¶ Synthesizes a propositional formula in the form of a single conjunctive clause that evaluates to
True
in the given model, and toFalse
in any other model over the same variable names.
-
propositions.semantics.
synthesize
(variables, values)¶ Synthesizes a propositional formula in DNF over the given variable names, that has the specified truth table.
- Parameters
variables (
Sequence
[str
]) – nonempty set of variable names for the synthesized formula.values (
Iterable
[bool
]) – iterable over truth values for the synthesized formula in every possible model over the given variable names, in the order returned byall_models
(
variables
)
.
- Return type
- Returns
The synthesized formula.
Examples
>>> formula = synthesize(['p', 'q'], [True, True, True, False]) >>> for model in all_models(['p', 'q']): ... evaluate(formula, model) True True True False
-
propositions.semantics.
_synthesize_for_all_except_model
(model)¶ Synthesizes a propositional formula in the form of a single disjunctive clause that evaluates to
False
in the given model, and toTrue
in any other model over the same variable names.
-
propositions.semantics.
synthesize_cnf
(variables, values)¶ Synthesizes a propositional formula in CNF over the given variable names, that has the specified truth table.
- Parameters
variables (
Sequence
[str
]) – nonempty set of variable names for the synthesized formula.values (
Iterable
[bool
]) – iterable over truth values for the synthesized formula in every possible model over the given variable names, in the order returned byall_models
(
variables
)
.
- Return type
- Returns
The synthesized formula.
Examples
>>> formula = synthesize_cnf(['p', 'q'], [True, True, True, False]) >>> for model in all_models(['p', 'q']): ... evaluate(formula, model) True True True False
-
propositions.semantics.
evaluate_inference
(rule, model)¶ Checks if the given inference rule holds in the given model.
- Parameters
rule (
InferenceRule
) – inference rule to check.
- Return type
- Returns
True
if the given inference rule holds in the given model,False
otherwise.
Examples
>>> evaluate_inference(InferenceRule([Formula('p')], Formula('q')), ... {'p': True, 'q': False}) False
>>> evaluate_inference(InferenceRule([Formula('p')], Formula('q')), ... {'p': False, 'q': False}) True
-
propositions.semantics.
is_sound_inference
(rule)¶ Checks if the given inference rule is sound, i.e., whether its conclusion is a semantically correct implication of its assumptions.
- Parameters
rule (
InferenceRule
) – inference rule to check.- Return type
- Returns
True
if the given inference rule is sound,False
otherwise.