How can I fix TypeError: BooleanAtom not allowed in this context

Viewed 35

I am trying to work with propositional logic using python. I have run the following program in google colaboratory.

import sympy
from sympy.abc import a,b,c
from sympy import S,simplify_logic

sympy.simplify(~a >> sympy.S.false).equals(sympy.S.true)

This results in the following error

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-72f54433199b> in <module
----> 1 sympy.simplify(~a >> sympy.S.false).equals(sympy.S.true)

1 frames
/usr/local/lib/python3.7/dist-packages/sympy/logic/boolalg.py in _noop(self, other)
    197 
    198 def _noop(self, other=None):
--> 199 raise TypeError('BooleanAtom not allowed in this context.')
    200 
    201 __add__ = _noop

TypeError: BooleanAtom not allowed in this context.

I expected false as the output.

Changing the expression to the following did not generate an error and output False as I expected.

sympy.simplify(a >> sympy.S.false).equals(sympy.S.true)

What is the difference? I want to know if two logical expressions containing true or false are equivalent. I read the documentation here but I did not find the solution.

1 Answers

The .equals method is supposed to be for comparing expressions rather than booleans. In this case what you want to know is if the two boolean statements are logically equivalent. For that you can use SymPy's Equivalent:

In [116]: from sympy.abc import a

In [117]: Equivalent(a >> False, True)
Out[117]: ¬a

This means that a >> False is equivalent to True if a is False but not otherwise. The statements are therefore not equivalent for all possible values of a.

Sometimes Equivalent can resolve to True or False if it can be shown that the statements are either always equal or always unequal for all possible values of a. It might be necessary to use simplify for a full resolution though:

In [119]: Equivalent(a >> False, False | a)
Out[119]: a ⇔ ¬a

In [120]: simplify(_)
Out[120]: False

In [121]: Equivalent(a >> False, False | ~a)
Out[121]: True
Related