propositions.syntax module¶
Syntactic handling of propositional formulas.
-
propositions.syntax.
is_variable
(string)¶ Checks if the given string is a variable name.
-
propositions.syntax.
is_constant
(string)¶ Checks if the given string is a constant.
-
propositions.syntax.
is_unary
(string)¶ Checks if the given string is a unary operator.
-
propositions.syntax.
is_binary
(string)¶ Checks if the given string is a binary operator.
-
class
propositions.syntax.
Formula
(root, first=None, second=None)¶ Bases:
object
An immutable propositional formula in tree representation, composed from variable names, and operators applied to them.
- Variables
-
__repr__
()¶ Computes the string representation of the current formula.
- Return type
- Returns
The standard string representation of the current formula.
-
__eq__
(other)¶ Compares the current formula with the given one.
-
__ne__
(other)¶ Compares the current formula with the given one.
-
variables
()¶ Finds all variable names in the current formula.
-
operators
()¶ Finds all operators in the current formula.
-
static
_parse_prefix
(string)¶ Parses a prefix of the given string into a formula.
- Parameters
string (
str
) – string to parse.- Return type
- Returns
A pair of the parsed formula and the unparsed suffix of the string. If the given string has as a prefix a variable name (e.g.,
'x12'
) or a unary operator followed by a variable name, then the parsed prefix will include that entire variable name (and not just a part of it, such as'x1'
). If no prefix of the given string is a valid standard string representation of a formula then returned pair should be ofNone
and an error message, where the error message is a string with some human-readable content.
-
static
is_formula
(string)¶ Checks if the given string is a valid representation of a formula.
-
static
parse
(string)¶ Parses the given valid string representation into a formula.
-
polish
()¶ Computes the polish notation representation of the current formula.
- Return type
- Returns
The polish notation representation of the current formula.
-
static
parse_polish
(string)¶ Parses the given polish notation representation into a formula.
-
substitute_variables
(substitution_map)¶ Substitutes in the current formula, each variable name
v
that is a key insubstitution_map
with the formulasubstitution_map[v]
.- Parameters
substitution_map (
Mapping
[str
,Formula
]) – mapping defining the substitutions to be performed.- Return type
- Returns
The formula resulting from performing all substitutions. Only variable name occurrences originating in the current formula are substituted (i.e., variable name occurrences originating in one of the specified substitutions are not subjected to additional substitutions).
Examples
>>> Formula.parse('((p->p)|r)').substitute_variables( ... {'p': Formula.parse('(q&r)'), 'r': Formula.parse('p')}) (((q&r)->(q&r))|p)
-
substitute_operators
(substitution_map)¶ Substitutes in the current formula, each constant or operator
op
that is a key insubstitution_map
with the formulasubstitution_map[op]
applied to its (zero or one or two) operands, where the first operand is used for every occurrence of'p'
in the formula and the second for every occurrence of'q'
.- Parameters
substitution_map (
Mapping
[str
,Formula
]) – mapping defining the substitutions to be performed.- Return type
- Returns
The formula resulting from performing all substitutions. Only operator occurrences originating in the current formula are substituted (i.e., operator occurrences originating in one of the specified substitutions are not subjected to additional substitutions).
Examples
>>> Formula.parse('((x&y)&~z)').substitute_operators( ... {'&': Formula.parse('~(~p|~q)')}) ~(~~(~x|~y)|~~z)