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: BinaryOp

Represents logical conjunction (AND, ∧).

Returns True if and only if both operands are True.

Parameters:
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: LogicalExpression

Represents 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

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:

BinaryOp

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
to_dict()[source]

Converts the binary operation to a dictionary.

Return type:

Dict

Returns:

Dictionary representation of the binary operation. Example: {“type”: “BinaryOp”, “left”: {“type”: “Proposition”, “name”: “P”}, “right”: {“type”: “Proposition”, “name”: “Q”}, “operator”: “AND”}

variables()[source]

Returns the list of variables in the binary operation.

Return type:

List[str]

Returns:

List of unique variable names from both operands.

agent_logic.core.Exists

alias of ExistentialQuantifier

agent_logic.core.ForAll

alias of UniversalQuantifier

class agent_logic.core.Function(**data)[source]

Bases: BaseModel

Represents a function f(x) in predicate logic.

Parameters:
  • name (str)

  • parameters (List[str])

  • function (Callable[[...], Any])

depth()[source]
Return type:

int

evaluate(context)[source]

Evaluate the function using context for parameter values.

Return type:

Any

Parameters:

context (Dict[str, Any])

classmethod from_dict(data)[source]
Return type:

Function

Parameters:

data (Dict)

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]
to_dict()[source]
Return type:

Dict

variables()[source]
Return type:

List[str]

class agent_logic.core.Iff(left, right)[source]

Bases: BinaryOp

Represents logical biconditional (IFF, ↔).

Returns True if both operands have the same truth value.

Parameters:
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: BinaryOp

Represents logical implication (IMPLIES, →).

Returns True if the left operand is False or the right operand is True.

Parameters:
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: BaseModel

Recursive 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:

LogicalExpression

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].

to_dict()[source]

Recursively converts expression to a dictionary.

Return type:

Dict

Returns:

Dictionary representation of the logical expression.

Raises:

NotImplementedError – This is an abstract method that must be implemented by subclasses.

variables()[source]

Recursively extracts all variables in the expression.

Return type:

List[str]

Returns:

List of variable names used in the expression.

Raises:

NotImplementedError – This is an abstract method that must be implemented by subclasses.

class agent_logic.core.Not(**data)[source]

Bases: LogicalExpression

Represents 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:

Not

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
to_dict()[source]

Converts the NOT expression to a dictionary.

Return type:

Dict

Returns:

Dictionary representation of the NOT expression. Example: {“type”: “Not”, “operand”: {“type”: “Proposition”, “name”: “P”}}

variables()[source]

Returns the list of variables in the NOT expression.

Return type:

List[str]

Returns:

List of variable names used in the operand.

class agent_logic.core.Or(left, right)[source]

Bases: BinaryOp

Represents logical disjunction (OR, ∨).

Returns True if at least one operand is True.

Parameters:
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: BaseModel

Represents a predicate in first-order logic.

Parameters:
  • name (str)

  • terms (List[Term])

depth()[source]
Return type:

int

evaluate(context)[source]

Evaluates the predicate given a context mapping variables to truth values.

Return type:

bool

Parameters:

context (Dict[str, bool])

classmethod from_dict(data)[source]
Return type:

Predicate

Parameters:

data (Dict)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str
terms: List[Term]
to_dict()[source]
Return type:

Dict

variables()[source]

Returns a list of variable names used in the predicate.

Return type:

List[str]

class agent_logic.core.Proposition(**data)[source]

Bases: LogicalExpression

Represents 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:

Proposition

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
to_dict()[source]

Converts the proposition to a dictionary.

Return type:

Dict

Returns:

Dictionary representation of the proposition. Example: {“type”: “Proposition”, “name”: “P”}

variables()[source]

Returns the list of variables in the proposition.

Return type:

List[str]

Returns:

A list containing only the name of this proposition.

class agent_logic.core.Term(**data)[source]

Bases: BaseModel

Represents 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: BinaryOp

Represents logical exclusive disjunction (XOR, ⊕).

Returns True if exactly one operand is True.

Parameters:
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

Base module for logical expressions.

functions

operations

Core logical operations module.

predicates

quantifiers