Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Expected behavior of logical expressions in Taichi #3635

Open
strongoier opened this issue Nov 28, 2021 · 5 comments
Open

[Discussion] Expected behavior of logical expressions in Taichi #3635

strongoier opened this issue Nov 28, 2021 · 5 comments
Labels
discussion Welcome discussion!

Comments

@strongoier
Copy link
Contributor

strongoier commented Nov 28, 2021

Currently, there isn't a clear description of expected behavior of logical expressions (and / or) in Taichi. The implementation,

logical_or = bit_or
logical_and = bit_and
, shows that logical_and / logical_or is simply treated as bitwise_and / bitwise_or. This leads to the non-short-circuit behavior (#3572) and the non-intuitive computation result:

@ti.kernel
def func(a: ti.i32, b: ti.i32) -> ti.i32:
    return a and b

print(func(2, 3))  # 2

In Python, the behavior of logical expressions is like:

print(2 and 3)  # 3
print(2.0 or 0)  # 2.0

which is short-circuiting and uses the determining expression as the result.

Should we make logical expressions in Taichi behave the same as in Python, keep the original behavior in Taichi, or define yet-another behavior? Opinions are welcome.

@lin-hitonami
Copy link
Contributor

I think the behavior should be identical to Python's because most Python users would expect taichi's grammar is similar to Python's.

@ailzhang
Copy link
Contributor

I second @lin-hitonami 's idea, python frontend's behavior should be close to python convention as much as possible. ;)

@strongoier
Copy link
Contributor Author

According to the discussions in this line, we will make the behavior of logical expressions in Taichi the same as Python.

@lin-hitonami
Copy link
Contributor

There is a problem if we make the behavior the same as Python. For this program:

@ti.kernel
def foo(a: ti.i32, b: ti.f32):
    c = a and b

What is the type of c? Will this confuse the type checker?

@strongoier
Copy link
Contributor Author

Yes this is indeed a problem. We cannot have exactly the same behavior as Python because Taichi is statically typed. In Taichi, all operands of logical expressions should be enforced to have a same type.

Then the core problem is more related to the order of our compilation process - we should do type check first, and then do transformations which may lose type enforcement information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Welcome discussion!
Projects
None yet
Development

No branches or pull requests

3 participants