Skip to content

Commit

Permalink
Merge pull request #20835 from JSS95/migrate_commonhandler
Browse files Browse the repository at this point in the history
refactor(assumptions): migrate common predicates to new design
  • Loading branch information
JSS95 committed Jan 22, 2021
2 parents 68c3266 + 3c6f63e commit 23d11f3
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 197 deletions.
6 changes: 0 additions & 6 deletions doc/src/modules/assumptions/handlers/common.rst

This file was deleted.

13 changes: 0 additions & 13 deletions doc/src/modules/assumptions/handlers/index.rst

This file was deleted.

1 change: 0 additions & 1 deletion doc/src/modules/assumptions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Contents
ask.rst
assume.rst
refine.rst
handlers/index.rst
predicates.rst


Expand Down
13 changes: 13 additions & 0 deletions doc/src/modules/assumptions/predicates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
Predicates
==========

Common
======

Tautological
------------

.. autoclass:: sympy.assumptions.predicates.common.IsTruePredicate

Commutative
-----------

.. autoclass:: sympy.assumptions.predicates.common.CommutativePredicate

Calculus
========

Expand Down
86 changes: 28 additions & 58 deletions sympy/assumptions/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sympy.logic.boolalg import (to_cnf, And, Not, Or, Implies, Equivalent)
from sympy.logic.inference import satisfiable
from sympy.utilities.decorator import memoize_property
from sympy.utilities.exceptions import SymPyDeprecationWarning
from sympy.assumptions.cnf import CNF, EncodedCNF, Literal


Expand Down Expand Up @@ -145,40 +146,13 @@ def composite(self):

@memoize_property
def commutative(self):
"""
Commutative predicate.
Explanation
===========
``ask(Q.commutative(x))`` is true iff ``x`` commutes with any other
object with respect to multiplication operation.
"""
# TODO: Add examples
return Predicate('commutative')
from .handlers.common import CommutativePredicate
return CommutativePredicate()

@memoize_property
def is_true(self):
"""
Generic predicate.
Explanation
===========
``ask(Q.is_true(x))`` is true iff ``x`` is true. This only makes
sense if ``x`` is a predicate.
Examples
========
>>> from sympy import ask, Q, symbols
>>> x = symbols('x')
>>> ask(Q.is_true(True))
True
"""
return Predicate('is_true')
from .handlers.common import IsTruePredicate
return IsTruePredicate()

@memoize_property
def symmetric(self):
Expand Down Expand Up @@ -463,22 +437,18 @@ def ask_full_inference(proposition, assumptions, known_facts_cnf):
def register_handler(key, handler):
"""
Register a handler in the ask system. key must be a string and handler a
class inheriting from AskHandler::
>>> from sympy.assumptions import register_handler, ask, Q
>>> from sympy.assumptions.handlers import AskHandler
>>> class MersenneHandler(AskHandler):
... # Mersenne numbers are in the form 2**n - 1, n integer
... @staticmethod
... def Integer(expr, assumptions):
... from sympy import log
... return ask(Q.integer(log(expr + 1, 2)))
>>> register_handler('mersenne', MersenneHandler)
>>> ask(Q.mersenne(7))
True
class inheriting from AskHandler.
.. deprecated:: 1.8.
Use multipledispatch handler instead. See :obj:`~.Predicate`.
"""
# Will be deprecated
SymPyDeprecationWarning(
feature="register_handler() function",
useinstead="multipledispatch handler of Predicate",
issue=20873,
deprecated_since_version="1.8"
).warn()
if isinstance(key, Predicate):
key = key.name.name
Qkey = getattr(Q, key, None)
Expand All @@ -489,8 +459,19 @@ class inheriting from AskHandler::


def remove_handler(key, handler):
"""Removes a handler from the ask system. Same syntax as register_handler"""
# Will be deprecated
"""
Removes a handler from the ask system. Same syntax as register_handler
.. deprecated:: 1.8.
Use multipledispatch handler instead. See :obj:`~.Predicate`.
"""
SymPyDeprecationWarning(
feature="remove_handler() function",
useinstead="multipledispatch handler of Predicate",
issue=20873,
deprecated_since_version="1.8"
).warn()
if isinstance(key, Predicate):
key = key.name.name
getattr(Q, key).remove_handler(handler)
Expand Down Expand Up @@ -575,17 +556,6 @@ def get_known_facts_dict():
for k, v in zip(keys, values)]) + ','
return fact_string % (p, c, m)

# handlers tells us what ask handler we should use
# for a particular key
_val_template = 'sympy.assumptions.handlers.%s'
_handlers = [
("commutative", "AskCommutativeHandler"),
("is_true", "common.TautologicalHandler"),
]

for name, value in _handlers:
register_handler(name, _val_template % value)

@cacheit
def get_known_facts_keys():
return [
Expand Down
15 changes: 13 additions & 2 deletions sympy/assumptions/assume.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sympy.multipledispatch.dispatcher import (
Dispatcher, MDNotImplementedError, str_signature
)
from sympy.utilities.exceptions import SymPyDeprecationWarning
from sympy.utilities.iterables import is_sequence
from sympy.utilities.source import get_class

Expand Down Expand Up @@ -417,11 +418,21 @@ def __call__(self, expr):
return AppliedPredicate(self, expr)

def add_handler(self, handler):
# Will be deprecated
SymPyDeprecationWarning(
feature="Predicate.add_handler() method",
useinstead="multipledispatch handler of Predicate",
issue=20873,
deprecated_since_version="1.8"
).warn()
self.handlers.append(handler)

def remove_handler(self, handler):
# Will be deprecated
SymPyDeprecationWarning(
feature="Predicate.remove_handler() method",
useinstead="multipledispatch handler of Predicate",
issue=20873,
deprecated_since_version="1.8"
).warn()
self.handlers.remove(handler)

def eval(self, args, assumptions=True):
Expand Down
6 changes: 3 additions & 3 deletions sympy/assumptions/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"""

from .common import (AskHandler, CommonHandler,
AskCommutativeHandler, TautologicalHandler, test_closed_group)
test_closed_group)

__all__ = [
'AskHandler', 'CommonHandler', 'AskCommutativeHandler',
'TautologicalHandler', 'test_closed_group'
'AskHandler', 'CommonHandler',
'test_closed_group'
]

0 comments on commit 23d11f3

Please sign in to comment.