bastikr / boolean.py

Implements boolean algebra in one module.
BSD 2-Clause "Simplified" License
78 stars 34 forks source link

Not able to simplify some simple expressions (eg. (0)&~((1)|(0)) ) #82

Open gabe-nasc opened 6 years ago

gabe-nasc commented 6 years ago

I've ran into a problem while using simplify with a few expressions, even though they are valid and have not apparent reason to cause these errors.

One thing I noticed is that if I remove the 'not' symbol from them, they'll work "correctly"

Cases where "TypeError: '_FALSE' object is not callable" is raised:

(0)&\~((1)|(0)) (1)&(\~((0)|(1))) (0)&(\~((0)|(0)))&(1)

Cases where "TypeError: '_TRUE' object is not callable" is raised:

(\~((0)&(0)))&(1)&(0)&(1) (0)|(0)|(\~((1)&(0)&(0)&(0)&(0))) (\~((1)&(0)&(1)&(1)))|(0)|(\~((0)|(0)|(1)))

MarcelRobitaille commented 5 years ago

I have reproduced this same issue. The minimal expression that causes this issue is 'NOT (FALSE)'. The problem is here. The self.TRUE and self.FALSE instances of DualBase are given instances not classes of _TRUE and _FALSE. I have come up with the following workaround:

algebra = boolean.BooleanAlgebra()
algebra.TRUE.dual = type(algebra.FALSE)
algebra.FALSE.dual = type(algebra.TRUE)

A more permanent solution would be to change the __init__ method of BooleanAlgebra like so:

self.TRUE_class = TRUE_class or _TRUE
self.TRUE = self.TRUE_class()

self.FALSE_class = FALSE_class or _FALSE
self.FALSE = self.FALSE_class()

# they cross-reference each other
self.TRUE.dual = self.FALSE_class
self.FALSE.dual = self.TRUE_class