def apply(self, axiom, *args, **kwargs): given = self if axiom.apply.__name__ == 'given': join = False elif 'join' in kwargs: join = kwargs['join'] del kwargs['join'] else: join = True if join and args and all(isinstance(eq, Boolean) for eq in args): args = [given, *args] given = And(*args, evaluate=False, equivalent=args) return given.apply(axiom, split=True, **kwargs) if 'depth' in kwargs: depth = kwargs['depth'] if not depth: del kwargs['depth'] if 'split' in kwargs: eqs = self.split() self, *rest = eqs args = tuple(rest) + args del kwargs['split'] return Boolean.apply(self, axiom, *args, **kwargs) else: kwargs['depth'] -= 1 else: return Boolean.apply(self, axiom, *args, **kwargs) return self.this.function.apply(axiom, *args, **kwargs)
def __new__(cls, expr, predicate=None, value=True): from sympy import Q if predicate is None: predicate = Q.is_true elif not isinstance(predicate, Predicate): key = str(predicate) try: predicate = getattr(Q, key) except AttributeError: predicate = Predicate(key) if value: return Boolean.__new__(cls, expr, predicate) else: return Not(Boolean.__new__(cls, expr, predicate))
def existent_symbols(self): free_symbols = Boolean.existent_symbols(self) bound_symbols = {var.base: var.indices for var in self.bound_symbols if var.is_Slice} if bound_symbols: deletes = set() for symbol in free_symbols: if symbol.is_Indexed: base = symbol.base if base in bound_symbols: slices = bound_symbols[base] if len(symbol.indices) == len(slices) and \ all(k >= b and b < e for k, (b, e) in zip(symbol.indices, slices)): deletes.add(symbol) free_symbols -= deletes return free_symbols
def evaluate(formula: Boolean, i: PropInt) -> bool: """ Evaluate a SymPy boolean expression against a propositional interpretation. The symbols not present in the propositional interpretation will be considered False. >>> from sympy.parsing.sympy_parser import parse_expr >>> evaluate(parse_expr("a & b"), {"a": True}) False >>> evaluate(parse_expr("a | b"), {"b": True}) True >>> evaluate(parse_expr("a"), {"b": True}) False :param formula: a sympy.logic.boolalg.Boolean. :param i: a propositional interpretation, i.e. a mapping from symbol identifiers to True/False :return: True if the formula is true in the interpretation, False o/w. """ return formula.subs(i).replace(Symbol, BooleanFalse) == BooleanTrue()
def test_double_negation(): a = Boolean() assert ~(~a) == a
def __new__(cls, predicate, arg): return Boolean.__new__(cls, predicate, arg)
def __new__(cls, name, handlers=None): obj = Boolean.__new__(cls) obj.name = name obj.handlers = handlers or [] return obj
def __new__(cls, predicate, arg): if not isinstance(arg, bool): # XXX: There is not yet a Basic type for True and False arg = _sympify(arg) return Boolean.__new__(cls, predicate, arg)
def __new__(cls, predicate, arg): arg = _sympify(arg) return Boolean.__new__(cls, predicate, arg)
def __and__(self, eq): """Overloading for & operator""" return Boolean.__and__(self, eq)