Skip to content

Commit

Permalink
fix: remove the TrueFormula and the FalseFormula to simplify the stru…
Browse files Browse the repository at this point in the history
…cture and fix the printing of empty Ands/Ors
  • Loading branch information
francescofuggitti committed Jun 21, 2023
1 parent 79c0d24 commit 496a254
Showing 1 changed file with 5 additions and 67 deletions.
72 changes: 5 additions & 67 deletions pddl/logic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,66 +115,6 @@ class Atomic(Formula):
"""Atomic formula."""


class TrueFormula(Formula):
"""A tautology."""

def __str__(self) -> str:
"""Get the string representation."""
return "(true)"

def __repr__(self) -> str:
"""Get an unambiguous string representation."""
return "TrueFormula()"

def __eq__(self, other):
"""Compare with another object."""
return isinstance(other, TrueFormula)

def __hash__(self):
"""Hash the object."""
return hash(TrueFormula)

def __invert__(self) -> Formula:
"""Negate the formula."""
return FALSE

def __neg__(self) -> Formula:
"""Negate."""
return FALSE


class FalseFormula(Formula):
"""A contradiction."""

def __str__(self) -> str:
"""Get the string representation."""
return "(false)"

def __repr__(self) -> str:
"""Get an unambiguous string representation."""
return "FalseFormula()"

def __eq__(self, other):
"""Compare with another object."""
return isinstance(other, FalseFormula)

def __hash__(self):
"""Hash the object."""
return hash(FalseFormula)

def __invert__(self) -> Formula:
"""Negate the formula."""
return TRUE

def __neg__(self) -> Formula:
"""Negate."""
return TRUE


TRUE = TrueFormula()
FALSE = FalseFormula()


class MonotoneOp(type):
"""Metaclass to simplify monotone operator instantiations."""

Expand All @@ -192,14 +132,14 @@ def __call__(cls, *args, **kwargs):
class And(BinaryOp, metaclass=MonotoneOp):
"""And operator."""

_absorbing = FALSE
_absorbing = False
SYMBOL = "and"


class Or(BinaryOp, metaclass=MonotoneOp):
"""Or operator."""

_absorbing = TRUE
_absorbing = True
SYMBOL = "or"


Expand Down Expand Up @@ -296,10 +236,10 @@ def ensure_formula(f: Optional[Formula], is_none_true: bool) -> Formula:
Ensure the argument is a formula.
:param f: the formula, or None.
:param is_none_true: if true, None reduces to TrueFormula; FalseFormula otherwise.
:param is_none_true: if true, None reduces to And(); FalseFormula otherwise.
:return: the same set, or an empty set if the arg was None.
"""
return f if f is not None else TrueFormula() if is_none_true else FalseFormula()
return f if f is not None else And() if is_none_true else Or()


def is_literal(formula: Formula) -> bool:
Expand All @@ -323,11 +263,9 @@ def is_literal(formula: Formula) -> bool:
def _simplify_monotone_op_operands(cls, *operands):
operands = list(dict.fromkeys(operands))
if len(operands) == 0:
return [~cls._absorbing]
return []
elif len(operands) == 1:
return [operands[0]]
elif cls._absorbing in operands:
return cls._absorbing

# shift-up subformulas with same operator. DFS on expression tree.
new_operands = []
Expand Down

0 comments on commit 496a254

Please sign in to comment.