agent_logic.core
Core logical expressions and operations.
This module provides the fundamental building blocks for creating and manipulating logical expressions in propositional and predicate logic.
- Classes:
LogicalExpression: Abstract base class for all logical expressions Proposition: A basic propositional variable Not: Logical negation operation BinaryOp: Base class for binary logical operations (And, Or, Implies, etc.) Quantifier: Base class for quantified expressions Predicate: Representation of predicate expressions Function: Representation of function terms
- class agent_logic.core.And(left, right)[source]
Bases:
BinaryOpRepresents logical conjunction (AND, ∧).
Returns True if and only if both operands are True.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agent_logic.core.BinaryOp(**data)[source]
Bases:
LogicalExpressionRepresents binary logical operations (AND, OR, IMPLIES, IFF).
Binary operations take two logical expressions as operands and combine them according to the specified operator.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
operator (Literal['AND', 'OR', 'IMPLIES', 'IFF'])
- left
The left operand of the binary operation.
- right
The right operand of the binary operation.
- operator
The type of binary operation, one of: - “AND”: logical conjunction (∧) - “OR”: logical disjunction (∨) - “IMPLIES”: logical implication (→) - “IFF”: logical biconditional (↔)
- depth()[source]
Returns the depth of the expression tree.
- Return type:
int- Returns:
The maximum depth of either operand plus 1.
- evaluate(context)[source]
Evaluates the binary operation given a truth assignment.
- Parameters:
context (
Dict[str,bool]) – Dictionary mapping variable names to truth values. Example: {“P”: True, “Q”: False}- Return type:
bool- Returns:
Boolean result of applying the binary operation to the operands.
- Raises:
ValueError – If the operator is unknown.
- classmethod from_dict(data)[source]
Reconstructs a binary operation from a dictionary.
- Parameters:
data (
Dict) – Dictionary representation of a binary operation. Must contain “left”, “right”, and “operator” fields.- Return type:
- Returns:
Reconstructed BinaryOp object.
- Raises:
ValueError – If the data is invalid or contains unknown operand types.
-
left:
LogicalExpression
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
operator:
Literal['AND','OR','IMPLIES','IFF']
-
right:
LogicalExpression
- agent_logic.core.Exists
alias of
ExistentialQuantifier
- agent_logic.core.ForAll
alias of
UniversalQuantifier
- class agent_logic.core.Function(**data)[source]
Bases:
BaseModelRepresents a function f(x) in predicate logic.
- Parameters:
name (str)
parameters (List[str])
function (Callable[[...], Any])
- evaluate(context)[source]
Evaluate the function using context for parameter values.
- Return type:
Any- Parameters:
context (Dict[str, Any])
- function: Callable[..., Any]
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- name: str
- parameters: List[str]
- class agent_logic.core.Iff(left, right)[source]
Bases:
BinaryOpRepresents logical biconditional (IFF, ↔).
Returns True if both operands have the same truth value.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agent_logic.core.Implies(left, right)[source]
Bases:
BinaryOpRepresents logical implication (IMPLIES, →).
Returns True if the left operand is False or the right operand is True.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agent_logic.core.LogicalExpression(**data)[source]
Bases:
BaseModelRecursive base class for logical expressions.
This abstract class defines the interface for all logical expressions in the system. Concrete implementations include Proposition, Not, and BinaryOp.
- model_config
Pydantic model configuration to allow arbitrary types.
- depth()[source]
Computes the depth of the logical expression tree.
- Return type:
int- Returns:
Integer representing the depth of the expression tree.
- Raises:
NotImplementedError – This is an abstract method that must be implemented by subclasses.
- evaluate(context)[source]
Recursively evaluates the expression under a given truth assignment.
- Parameters:
context (
Dict[str,bool]) – Dictionary mapping variable names to truth values. Example: {“P”: True, “Q”: False}- Return type:
bool- Returns:
Boolean result of evaluating the expression.
- Raises:
NotImplementedError – This is an abstract method that must be implemented by subclasses.
- classmethod from_dict(data)[source]
Recursively reconstructs an expression from a dictionary.
- Parameters:
data (
Dict) – Dictionary representation of a logical expression. Must include a ‘type’ field.- Return type:
- Returns:
Reconstructed logical expression.
- Raises:
ValueError – If the data is not a dictionary, doesn’t have a type field, or has an unknown expression type.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agent_logic.core.Not(**data)[source]
Bases:
LogicalExpressionRepresents logical negation (¬).
The NOT operation negates the truth value of its operand.
- Parameters:
operand (LogicalExpression)
- operand
The logical expression being negated.
- depth()[source]
Returns the depth of the expression tree.
- Return type:
int- Returns:
The depth of the operand plus 1.
- evaluate(context)[source]
Evaluates the NOT expression given a truth assignment.
- Parameters:
context (
Dict[str,bool]) – Dictionary mapping variable names to truth values. Example: {“P”: True, “Q”: False}- Return type:
bool- Returns:
Boolean result of negating the operand’s evaluation.
- classmethod from_dict(data)[source]
Reconstructs a NOT expression from a dictionary.
- Parameters:
data (
Dict) – Dictionary representation of a NOT expression. Must contain an “operand” field.- Return type:
- Returns:
Reconstructed Not object.
- Raises:
ValueError – If the data is invalid or contains an unknown operand type.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
operand:
LogicalExpression
- class agent_logic.core.Or(left, right)[source]
Bases:
BinaryOpRepresents logical disjunction (OR, ∨).
Returns True if at least one operand is True.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agent_logic.core.Predicate(**data)[source]
Bases:
BaseModelRepresents a predicate in first-order logic.
- Parameters:
name (str)
terms (List[Term])
- evaluate(context)[source]
Evaluates the predicate given a context mapping variables to truth values.
- Return type:
bool- Parameters:
context (Dict[str, bool])
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- name: str
- terms: List[Term]
- class agent_logic.core.Proposition(**data)[source]
Bases:
LogicalExpressionRepresents an atomic proposition (logical variable).
An atomic proposition is the most basic unit in propositional logic, representing a statement that can be either true or false.
- Parameters:
name (str)
- name
The name of the proposition (e.g., “P”, “Q”).
- depth()[source]
Returns the depth of the expression tree.
For an atomic proposition, the depth is always 0.
- Return type:
int- Returns:
0, as atomic propositions have no depth.
- evaluate(context)[source]
Evaluates the proposition given a truth assignment.
- Parameters:
context (
Dict[str,bool]) – Dictionary mapping variable names to truth values. Example: {“P”: True, “Q”: False}- Return type:
bool- Returns:
Boolean value of the proposition in the given context.
- Raises:
ValueError – If the proposition name is not in the context.
- classmethod from_dict(data)[source]
Reconstructs a proposition from a dictionary.
- Parameters:
data (
Dict) – Dictionary representation of a proposition. Must contain a “name” field.- Return type:
- Returns:
Reconstructed Proposition object.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
name:
str
- class agent_logic.core.Term(**data)[source]
Bases:
BaseModelRepresents a term in predicate logic (constants, variables, functions).
- Parameters:
value (str | List[Term])
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- value: Union[str, List['Term']]
- class agent_logic.core.Xor(left, right)[source]
Bases:
BinaryOpRepresents logical exclusive disjunction (XOR, ⊕).
Returns True if exactly one operand is True.
- Parameters:
left (LogicalExpression)
right (LogicalExpression)
- evaluate(context)[source]
Evaluates the XOR operation given a truth assignment.
- Parameters:
context (
Dict[str,bool]) – Dictionary mapping variable names to truth values.- Return type:
bool- Returns:
Boolean result of XOR operation (True if exactly one operand is True).
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Modules
Base module for logical expressions. |
|
Core logical operations module. |
|