Teg icon indicating copy to clipboard operation
Teg copied to clipboard

Broken boolean overrides

Open dromaiidae opened this issue 4 years ago • 3 comments

Boolean operator overrides are parsed incorrectly; currently you have to explicitly construct an And object to use it

Here's an example unittest to see this

def test_bool_override(self):
    from teg.lang.integrable_program import And
    x = Var('x', 0)
    a = And(1 < x, x < 2)
    b = 1 < x and x < 2
    c = 1 < x < 2
    print(f'{a}:\t{evaluate(a)}')
    print(f'{b}:\t{evaluate(b)}')
    print(f'{c}:\t{evaluate(c)}')
    self.assertEqual(a, b)
    self.assertEqual(a, c)

dromaiidae avatar Dec 18 '20 11:12 dromaiidae

Unfortunately, these warts are part of the price that we pay for living in Python rather than writing a parser. You can use b = (1 < x) & (x < 2) instead though!

b = 1 < x and x < 2

Due to short-circuiting, the and operator cannot be overloaded.

c = 1 < x < 2

Short-circuiting is the issue here as well. To test, run

def f():
     print('hi')
     return 0
1 < 0 < f()

martinjm97 avatar Dec 18 '20 13:12 martinjm97

As a result, I'd view this less as a bug and more as a factor in deciding what our frontend should be in the end.

martinjm97 avatar Dec 18 '20 13:12 martinjm97

Yes, it does look like there are AST hacks that work. For example, https://stackoverflow.com/questions/25469423/defining-new-semantics-for-expressions-in-python/25469702#25469702

martinjm97 avatar Dec 18 '20 20:12 martinjm97