cjdrake / pyeda

Python EDA
BSD 2-Clause "Simplified" License
304 stars 54 forks source link

something wrong with espresso #124

Closed E-Neo closed 9 years ago

E-Neo commented 9 years ago

There may be something wrong with Espresso:

A, B, C = map(exprvar, 'ABC') f = truthtable((A, B, C), '10110101') expr2truthtable(espresso_tts(f)[0]) == f

It should return True but it returned False

cjdrake commented 9 years ago

The == operator in this context only tells you whether the Python object returned by the expr2truthtable function is the same object as f. They will not be the same, since the function returns a new object. In other words, the two truth tables are functionally equivalent, but the Python interpreter is telling you that they are two separate objects in memory.

Try this instead:

>>> a, b, c = map(exprvar, 'abc')
>>> f_tt = truthtable([a, b, c], "10110101")
>>> f_ex = truthtable2expr(f_tt)
>>> g_ex = espresso_tts(f_tt)[0]
# Show the expressions
>>> f_ex
Or(And(~a, ~b, ~c), And(~a, b, ~c), And(a, b, ~c), And(a, ~b, c), And(a, b, c))
>>> g_ex
Or(And(b, ~c), And(~a, ~c), And(a, c))
# Now test equivalence
>>> f_ex.equivalent(g_ex)
True

Incidentally, it is not a good idea to change the behavior of the == operator, or any other comparison operator. Those operators are used by Python for sorting and hashing, so not a good idea to change their meaning unless you know what you're doing.