SEPLogic¶
- Author
Marcel Simader
- Date
13.07.2021
New in version 1.3.0.
- SEPModules.maths.SEPLogic.Assignment¶
A type alias for a mapping of
AtomicPropositionobjects to abooltruth value.alias of
Mapping[SEPModules.maths.SEPLogic.AtomicProposition,bool]
Exceptions¶
- exception SEPModules.maths.SEPLogic.LogicError(msg, proposition)¶
LogicErroris an exception class for errors in theSEPLogicmodule.- Parameters
msg (AnyStr) – the message of the error
proposition (AnyStr) – the string representation of the proposition which is related to the error
- exception SEPModules.maths.SEPLogic.LogicSyntaxError(msg, proposition, file_path, lineno, offset, code_context)¶
LogicSyntaxErroris an exception class for syntax errors in theSEPLogicmodule.- Parameters
msg (AnyStr) – the message of the error
proposition (AnyStr) – the string representation of the proposition which is related to the error
file_path (Union[AnyStr, os.PathLike]) – the path to the file where the error occurred
lineno (int) – the line number at which the error occurred in the file
offset (int) – the offset in characters for the syntax error ‘cursor’
code_context (List[AnyStr]) – the code context of where the error occurred (i.e. the source code)
- static from_traceback(msg, proposition, tb, offset=0)¶
Creates a
SEPSyntaxErrorobject from a message, traceback, and an optional offset.- Parameters
msg (AnyStr) – the message of the error
proposition (AnyStr) – the string representation of the proposition which is related to the error
tb (inspect.Traceback) – the traceback to use as basis for the error
offset (int) – the offset in characters for the syntax error ‘cursor’
- Returns
a new
SEPSyntaxErrorinstance- Return type
Connective Bases¶
- class SEPModules.maths.SEPLogic._Connective(value)¶
_Connectiveis the base class for connective enumerations. Each enumeration member must contain the method that this connective is associated with. It also stores an arity (seeConnectiveArity), and a “strength”.The relational operators
<,>,<=,>=of this instance compare thestrengthattribute, and the equivalency operators==and!=compare strict object identity.- Parameters
method – the method corresponding to this connective
arity – the arity of this connective (see
ConnectiveArity)strength – an integer value defining this connective’s relative strength to the other connectives (for instance, negation is stronger than material implication)
- property method: Callable¶
- Returns
the method implementing the behavior of this connective
- property arity: SEPModules.maths.SEPLogic.ConnectiveArity¶
- Returns
the arity of this connective
- property strength: int¶
- Returns
the relative strength of this connective
- final class SEPModules.maths.SEPLogic.ConnectiveArity(value)¶
ConnectiveArityis an enum representing the arity of a_Connectiveobject. It contains an operand checking function which takes in the number of operands and returns whether this number is valid with the operator, and a description string.The available members are as follows:
NILARY: exactly 0 operands (name is derived from word nil)UNARY: exactly 1 operandBINARY: exactly 2 operandsNARY: 2 or more operands (n-ary)
- Param
a function which checks with the supplied number of operands whether the operator can be applied or not
- Parameters
description – a short description of this arity for use in error and log output
- property operand_check: Callable[[int], bool]¶
- Returns
a function which checks if the supplied number of operands is valid
- property description: str¶
- Returns
a short description of this arity for use in log or error output
- final class SEPModules.maths.SEPLogic.ConnectiveFormat(enumeration, entries)¶
ConnectiveFormatis a class which holds a mapping of_Connectiveobjects to their respectiveConnectiveFormat.Entryobjects.- Parameters
enumeration (Type[_ConnectiveEnum]) – which
_Connectiveenumeration to define in this formatentries (Mapping[_ConnectiveEnum, ConnectiveFormat.Entry]) – a mapping of
_Connectiveobjects to their respectiveConnectiveFormat.Entryobjects.
- class Entry(prefix, joiner, suffix)¶
ConnectiveFormat.Entryholds one entry of the mapping ofConnectiveFormat.- Parameters
prefix (str) – the string to prepend to the operands
joiner (str) – the string to use to join the operands (see
str.join)suffix (str) – the string o append to the operands
- property prefix: str¶
The string to prepend to the operands.
- property joiner: str¶
The string to use to join the operands (see
str.join).
- property suffix: str¶
The string to append to the operands.
- property enumeration: Type[_ConnectiveEnum]¶
The
_Connectiveenumeration this format defines.
- format(*operands, connective)¶
Formats the operands with the given connective based on the format specified by the mapping in this class.
- Parameters
operands (str) – an arbitrary number of operands as strings
connective (_ConnectiveEnum) – the connective from the specified
enumerationenum class
- Returns
the formatted string
- Return type
str
Logical Connective Enum¶
- final class SEPModules.maths.SEPLogic.LogicalConnective(value)¶
LogicalConnectiveis an enum deriving from_Connective. it contains the basic logical connectives:EMPTY: no operator, empty (nilary)NONE: no operator, identity (unary)NEG: negation (unary)EXIST: existential quantification (unary)UNIV: universal quantification (unary)AND: logical and (n-ary)OR: logical or (n-ary)R_IMPL: material “left-implies-right” implication (binary)L_IMPL: material “right-implies-left” implication (binary)IFF: material equivalence, biconditional “if-and-only-if” (binary)
- class SEPModules.maths.SEPLogic.SupportsLogicalConnective(*args, **kwargs)¶
A simple protocol for indicating that the standard logical connective methods are supported by a class.
Connective Format Protocols¶
- class SEPModules.maths.SEPLogic.SupportsConnectiveFormat(*args, **kwargs)¶
Protocol to check whether or not a class supports formatting by
ConnectiveFormat.
- class SEPModules.maths.SEPLogic.SupportsToPrettyPrint(*args, **kwargs)¶
Abstract base class for subclasses that can convert themselves to a pretty printed string.
- to_pretty_print()¶
Converts this object to a pretty-printed string.
- Returns
a pretty-printed string representation of this object
- Return type
str
- class SEPModules.maths.SEPLogic.SupportsToLimboole(*args, **kwargs)¶
Abstract base class for subclasses that can convert themselves to the limboole syntax as string.
- to_limboole()¶
Converts this object to a limboole-compatible string.
- Returns
a limboole-compatible string representation of this object
- Return type
str
Proposition Protocols¶
- class SEPModules.maths.SEPLogic.SupportsEval(*args, **kwargs)¶
Protocol to check whether or not an object can be evaluated using an assignment.
- abstract eval(assignment)¶
Evaluates this object using assignment
assignmentand returns its truth value.- Parameters
assignment (Assignment) – the mapping (assignment) of truth values to
AtomicPropositionobjects- Returns
the truth value of this object under assignment
assignment- Return type
bool
- class SEPModules.maths.SEPLogic.SupportsLimbooleEval(*args, **kwargs)¶
Abstract base class for an object that can be converted to limboole syntax and then evaluated by limboole.
- limboole_eval(*option, timeout=1.0)¶
Passes the value generated by
to_limboole()to theSTDINof a Pythonsubprocesscall to the programlimboolewith argumentsoption. Additionally, a timeout is specified to abort waiting for the program to return.- Parameters
option (str) – an arbitrary amount of strings which will be added to the called limboole command
timeout (Optional[float]) – a timeout in seconds after which the execution of the subprocess will be halted and a
RuntimeErrorraised
- Returns
the
STDOUTof the program- Raises
LogicError – if there was an error while running the program, if the process timed out after the seconds specified by the
timeoutargument- Return type
str
Propositions¶
Proposition¶
- class SEPModules.maths.SEPLogic.Proposition(*proposition, connective=None)¶
Propositionis the base class for representing a general propositional object. A general proposition consists of an arbitrary amount of compositePropositionobjects connected by a_Connective. Upon creation of a proposition, the connective’s arity operand check function is called to ensure the correct number of composite propositions.Aside from the propositions and connective, this class also keeps track of all of the seen connectives in its child proposition objects. It also keeps track of all the atomic propositions that have been seen in its child proposition objects (see
AtomicProposition).The class provides the following attributes for checking various characteristics of the structure of the proposition:
empty,atomic,literal,quantifier,quantified.The operations defined by
LogicalConnectivehave been implemented in this class, which means that all propositions can be arbitrarily combined, with the mapping from operator to method being outlined in each member object ofLogicalConnective. It is noteworthy to say that__mul__(*) is mapped to__and__(&), and__add__(+) is mapped to__or__(|) by default.Note
All new instances of
Propositionare normalized according to the static methodnormalize(), so that redundant constructions like nestedLogicalConnective.NONEconnectives can be avoided.Warning
The boolean value of all
Propositionobjects isFalseby default and issues a syntax warning. If one desires to check the truth value of a proposition, theeval()method should be used with an assignment.- Parameters
proposition – an arbitrary number of
Propositionobjectsconnective – a
_Connectivefor defining the combining behavior of all passed propositions, usually a member ofLogicalConnective, ifNoneis given the constructor will try to use the correct connective when possible
- Raises
ValueError – if no connective is given and the connective cannot be deduced from the other arguments, if the number of operands is incompatible with the given connective
- static normalize(propositions, connective)¶
Normalizes the input to the initializer of a
Propositioninstance. The arguments to this function are the same arguments passed to__init__()but note that this function will not try to check or correct these inputs. The following rules are applied for \(a \in \mathcal{P}\), and \(P \in \mathcal{L}\):\[\begin{split}\neg \top &{}\Rightarrow \bot \\ \neg \bot &{}\Rightarrow \top \\ \neg \neg a &{}\Rightarrow a \\ (a) &{}\Rightarrow a \\ P \land (a \land \cdots) &{}\Rightarrow P \land a \land \cdots \\ P \lor (a \lor \cdots) &{}\Rightarrow P \lor a \lor \cdots\end{split}\]Note that in this notation, the parentheses \((\cdots)\) refer to a
Propositionobject which is nested within the unaryLogicalConnective.NONEconnective.
- property propositions: Tuple[SEPModules.maths.SEPLogic.Proposition]¶
- Returns
the child proposition instances of this class
- property connective: SEPModules.maths.SEPLogic._Connective¶
- Returns
the connective of this class, or
LogicalConnective.NONEif no connective was defined
- property seen_atomic_propositions: Set[SEPModules.maths.SEPLogic.AtomicProposition]¶
Gathers a set of all the atomic propositions that have been defined in any of the child proposition objects of this class.
- Returns
a set of atomic propositions found in any of the child propositions of this class
- property seen_connectives: Set[SEPModules.maths.SEPLogic._Connective]¶
Gathers a set of all the connectives that have been defined in any of the child proposition objects of this class.
- Returns
a set of connectives found in any of the child propositions of this class
- property empty: bool¶
- Returns
whether or not this proposition contains child propositions
- property atomic: bool¶
- Returns
whether or not this proposition is atomic
- property literal: bool¶
- Returns
whether or not this proposition is a literal (i.e. atomic or a negation of an atomic proposition)
- property quantifier: bool¶
- Returns
whether or not this proposition is a quantifier (i.e. a
literalproposition which is existentially or universally quantified)
- property quantified: bool¶
- Returns
whether or not this proposition contains any quantifiers (i.e. a proposition where any of the child propositions fulfill
quantifier)
- CNF()¶
Checks whether or not this formulas is in conjunctive normal form (counterpart to
DNF()).- Returns
whether or not this formula is in CNF
- Return type
bool
- DNF()¶
Checks whether or not this formulas is in disjunctive normal form (counterpart to
CNF()).- Returns
whether or not this formula is in DNF
- Return type
bool
- valid()¶
Checks whether or not this formula is valid using
limboole.- Returns
whether or not this formula is valid
- Raises
LogicError – if the proposition is quantified
- Return type
bool
- sat()¶
Checks whether or not this formula is satisfiable using
limboole.- Returns
whether or not this formula is satisfiable
- Raises
LogicError – if the proposition is quantified
- Return type
bool
- model()¶
Looks for a satisfying assignment (model) of this formula using
limboole. If no such model is found and the formula is unsatisfiable, the empty dictionary is returned. The dictionary maps eachAtomicPropositionobject of this class to a boolean truth value.- Returns
either a satisfying assignment as dictionary or the empty dictionary, if no such assignment exists
- Raises
LogicError – if there was an error parsing the output model
- Return type
Assignment
- eval(assignment)¶
Evaluates this object using assignment
assignmentand returns its truth value.- Parameters
assignment (Assignment) – the mapping (assignment) of truth values to
AtomicPropositionobjects- Returns
the truth value of this object under assignment
assignment- Return type
bool
- partial_eval(assignment, *, simplify=False)¶
Partially evaluate this proposition by replacing atomic propositions by the truth constants
Top, orBottomaccording to the given assignment.See also
- Parameters
assignment (Assignment) – the (partial) assignment to apply to this proposition
simplify (bool) – whether or not to apply simplification rules to the proposition after applying the assignment
- Returns
the partially evaluated proposition
- Raises
LogicError – if the proposition is quantified
- Return type
- simplify_neg()¶
Perform simplification steps regarding the negations in this proposition. The following rules are applied for \(a, b \in \mathcal{P}\), and a proposition \(P \in \mathcal{L}\):
\[\begin{split}\neg \neg a &{}\Longrightarrow a \\ \neg (a \rightarrow b) &{}\Longrightarrow a \lor \neg b \\ \neg (a \leftarrow b) &{}\Longrightarrow \neg a \lor b \\ \neg (a \leftrightarrow b) &{}\Longrightarrow (a \lor b) \land (\neg a \lor \neg b) \\ \neg \exists a : P &{}\Longrightarrow \forall a : \neg P \\ \neg \forall a : P &{}\Longrightarrow \exists a : \neg P \\ \neg (a \land b \land \cdots) &{}\Longrightarrow \neg a \lor \neg b \lor \cdots \\ \neg (a \lor b \lor \cdots) &{}\Longrightarrow \neg a \land \neg b \land \cdots\end{split}\]- Returns
the simplified proposition
- Return type
- reduce()¶
Reduce this proposition, so that its syntactic representation is more compact. The following rules are applied for \(a, b \in \mathcal{P}\), and propositions \(P, Q, R, S \in \mathcal{L}\):
\[a \leftarrow b :\Longleftrightarrow b \rightarrow a\]\[\begin{split}\bot \rightarrow a &{}\Longrightarrow \top \\ a \rightarrow \top &{}\Longrightarrow \top \\ a \rightarrow a &{}\Longrightarrow \top \\ a \land b \land \cdots \rightarrow a &{}\Longrightarrow \top \\ a \rightarrow a \lor b \lor \dots &{}\Longrightarrow \top \\ a \rightarrow \bot &{}\Longrightarrow \neg a \\ \top \rightarrow a &{}\Longrightarrow a \\ a \rightarrow \neg a \land b \land \cdots &{}\Longrightarrow \neg a \\ \neg a \rightarrow a \land b \land \cdots &{}\Longrightarrow a \\ a \lor b \lor \cdots \rightarrow \neg a &{}\Longrightarrow \neg a \\ \neg a \lor b \lor \cdots \rightarrow a &{}\Longrightarrow a\end{split}\]\[\begin{split}a \leftrightarrow \top &{}\Longrightarrow a \\ \top \leftrightarrow a &{}\Longrightarrow a \\ a \leftrightarrow \bot &{}\Longrightarrow \neg a \\ \bot \leftrightarrow a &{}\Longrightarrow \neg a \\ a \leftrightarrow a &{}\Longrightarrow \top \\ \neg a \leftrightarrow a &{}\Longrightarrow \bot \\ a \leftrightarrow \neg a &{}\Longrightarrow \bot\end{split}\]\[\begin{split}\top \land P &{}\Longrightarrow P \\ \bot \land \cdots &{}\Longrightarrow \bot \\ \top \lor \cdots &{}\Longrightarrow \top \\ \bot \lor P &{}\Longrightarrow P \\ a \lor \neg a \lor \cdots &{}\Longrightarrow \top \\ a \land \neg a \land \cdots &{}\Longrightarrow \bot \\ a \land (a \lor \cdots) \land P &{}\Longrightarrow a \land P \\ a \lor (a \land \cdots) \lor P &{}\Longrightarrow a \lor P \\ (P \land Q) \lor (P \land R) \lor S &{}\Longrightarrow \big(P \land (Q \lor R)\big) \lor S \\ (P \lor Q) \land (P \lor R) \land S &{}\Longrightarrow \big(P \lor (Q \land R)\big) \land S \\ \neg P \lor Q \lor R &{}\Longrightarrow (P \rightarrow Q) \lor R \\ P \lor \neg Q \lor R &{}\Longrightarrow (Q \rightarrow P) \lor R \\ (P \rightarrow Q) \land (Q \rightarrow P) \land R &{}\Longrightarrow (P \leftrightarrow Q) \land R\end{split}\]- Returns
the reduced proposition
- Return type
- expand()¶
Expand this proposition, so that its syntactic representation is less compact. The following rules are applied for \(a, b, c \in \mathcal{P}\), and a proposition \(P \in \mathcal{L}\):
\[\begin{split}a \rightarrow b &{}\Longrightarrow \neg a \lor b \\ a \leftarrow b &{}\Longrightarrow a \lor \neg b \\ a \leftrightarrow b &{}\Longrightarrow (a \land b) \lor (\neg a \land \neg b) \\ a \land (b \lor c \lor \cdots) \land P &{}\Longrightarrow \big((a \land b) \lor (a \land c) \lor \cdots\big) \land P \\ a \lor (b \land c \land \cdots) \lor P &{}\Longrightarrow \big((a \lor b) \land (a \lor c) \land \cdots\big) \lor P\end{split}\]- Returns
the expanded proposition
- Return type
- copy()¶
Copies this proposition into a new proposition object. This is a deep copy of all containing propositions.
- Returns
a deep copy of this proposition object
- Return type
- syntactically_equal(other)¶
Compare two propositions syntactically based on their computed hash.
- Parameters
other – the proposition to compare against
- Returns
whether or not
clsis syntactically equivalent toother- Return type
bool
Atomic Proposition¶
- class SEPModules.maths.SEPLogic.AtomicProposition¶
AtomicPropositionrepresents the smallest unit of aProposition, aside from the empty formula. Each new instance ofAtomicPropositionis uniquely assigned aniddefined by the system’s current time in nanoseconds and a random __offset. The class registers this id in a set to avoid the slim chance of a duplicate. All subclasses should, if they desire a custom implementation of the id system, register it like so:AtomicProposition._id_set.add(id).Each
AtomicPropositionobject, besides theid, also has a uniquevolatile_name. As the name suggests, this name does not guarantee to be unique over multiple runtimes and is subject to change at any point. It is used when generating theto_limboole()and__str__()format outputs. The naming convention is given as regular expression^[a-z](')*$for pretty-printed, and^[a-z](-prime)*$for limboole output.This class, as well as each class subclassing
AtomicPropositionwill contain the class attributes defining how thevolatile_nameof an object should behave. Each new subclass will start the name back at the lowest character at the lowest “prime characters” count. To keep names unique, each name will be prefixed with the subclass’ name. This behaviour may be changed but with caution.To get the next volatile name for a new object, use the class method
_next_volatile_name(). To get the next id for a new object, use the static method_next_id().The exception are the special
TopandBottomsingleton objects.Topwill always be assigned id1and volatile nametop, andBottomwill always be assigned id0and volatile namebottom. The pretty-printed outputs oftopandbottomare the down-tick and up-tick symbols respectively, and the limboole outputs are(top | !top)and(bottom & !bottom)since limboole does not support these truth values natively.- static normalize(propositions, connective)¶
Normalizes the input to the initializer of a
Propositioninstance. The arguments to this function are the same arguments passed to__init__()but note that this function will not try to check or correct these inputs. The following rules are applied for \(a \in \mathcal{P}\), and \(P \in \mathcal{L}\):\[\begin{split}\neg \top &{}\Rightarrow \bot \\ \neg \bot &{}\Rightarrow \top \\ \neg \neg a &{}\Rightarrow a \\ (a) &{}\Rightarrow a \\ P \land (a \land \cdots) &{}\Rightarrow P \land a \land \cdots \\ P \lor (a \lor \cdots) &{}\Rightarrow P \lor a \lor \cdots\end{split}\]Note that in this notation, the parentheses \((\cdots)\) refer to a
Propositionobject which is nested within the unaryLogicalConnective.NONEconnective.
- final static _next_id(requested_id=None)¶
Get the next available id. If
requested_idis passed, then that id will be returned if it is valid and has not been used by another object.- Parameters
requested_id (Optional[int]) – optional, the requested id
- Returns
the requested id or the next valid id if no requested id was passed or if the requested id was taken already
- Return type
int
- classmethod _next_volatile_name()¶
Get the next available volatile name.
- Returns
the next volatile name as string
- Return type
str
- property id: int¶
- Returns
the id of this atomic proposition object
- property volatile_name: str¶
- Returns
the volatile name of this atomic proposition object
- eval(assignment)¶
Evaluates this object using assignment
assignmentand returns its truth value.- Parameters
assignment (Assignment) – the mapping (assignment) of truth values to
AtomicPropositionobjects- Returns
the truth value of this object under assignment
assignment- Return type
bool
- copy()¶
Copies this proposition into a new proposition object. This is a deep copy of all containing propositions.
- Returns
a deep copy of this proposition object
- Return type