Fun with the new Logic module
The logic module is slowly becoming useful. This week I managed to get some basic inference in propositional logic working. This should be enough for the assumption sysmtem (although having first-order inference would be cool).
You can pull from my branch:
Here are some examples of what it can do:
First, importing and defining our symbols
In [1]: A, B, C = symbols('ABC')
In [2]: from sympy.logic import *
It works with Symbols just as you would expect
In [3]: And(A, B) Out[3]: And(A, B)
It applies De Morgan Rules automatically
In [4]: Not(Or(A, B)) Out[4]: And(Not(A), Not(B))
converts to conjuntive normal form (CNF)
In [5]: to_cnf(Implies(A, And(B, C))) Out[5]: And(Or(B, Not(A)), Or(C, Not(A)))
Some basic inference:
In [6]: pl_true( Or(A, B), {A : True}) # what can we say about Or(A, B) if A is True ?
Out[6]: True
In [7]: pl_true ( And(A, B), {B: False}) # what is And(A, B) if B is False
Out[7]: False
To be discussed:
- I’m not sure if we should override &&, || on Symbol so that we can do A && B instead of And(A, B). If would make the code
cleaner, but also I don’t want to bloat Symbol any more. What do you think ?
I’m very proud of this in the sense that it is a nice clean module that will hopefully serve as the foundation of the new assumption
system.
May 31st, 2009 at 11:31 pm
I got this warning:
In [7]: to_cnf(Implies(A, And(B, C)))
sympy/logic/boolalg.py:93: DeprecationWarning: object.__init__() takes no parameters
return super(BooleanFunction, self).__init__(*args, **kwargs)
Out[7]: And(Or(B, Not(A)), Or(C, Not(A)))
otherwise it works fine, the following tests failed:
_______________ sympy/logic/tests/test_boolalg.py:test_And_bool ________________
File “/home/ondrej/repos/sympy/sympy/logic/tests/test_boolalg.py”, line 14, in test_And_bool
assert And(True, True ) == True
AssertionError
________________________________________________________________________________
________________ sympy/logic/tests/test_boolalg.py:test_Or_bool ________________
File “/home/ondrej/repos/sympy/sympy/logic/tests/test_boolalg.py”, line 19, in test_Or_bool
assert Or(True, True ) == True
AssertionError
________________________________________________________________________________
_______________ sympy/logic/tests/test_boolalg.py:test_Not_bool ________________
File “/home/ondrej/repos/sympy/sympy/logic/tests/test_boolalg.py”, line 24, in test_Not_bool
assert Not(True, True ) == [False, False]
AssertionError
tests finished: 1458 passed, 4 failed, 4 skipped, 28 xfailed, 1 xpassed in 100.06 seconds
and 5 doctests failed, just run:
$ bin/doctest
[…]
====== tests finished: 209 passed, 4 failed, 1 exceptions in 2.48 seconds ======
DO *NOT* COMMIT!
May 31st, 2009 at 11:37 pm
As to overriding || and &&, I think we should and it should be overridden in the Basic class.
June 17th, 2009 at 4:47 pm
Why don’t you override the operators for and, or, etc, so you could use the python keywords? I believe they are here: http://docs.python.org/reference/datamodel.html#emulating-numeric-types
June 19th, 2009 at 12:20 pm
Yes, that is done now