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

Identity element for logic operations #80

Closed
CiottiGiorgio opened this issue Jul 26, 2021 · 2 comments
Closed

Identity element for logic operations #80

CiottiGiorgio opened this issue Jul 26, 2021 · 2 comments
Labels

Comments

@CiottiGiorgio
Copy link
Contributor

CiottiGiorgio commented Jul 26, 2021

Problem

Right now if I write Int(1) as argument of And it gets actually compiled to the code. It would be nice if the compiler could calculate that an identity element doesn't change the result of the operation.
For example if I want to insert a check depending on the value of some parameter I actually have to change the whole expression.

return core_checks if value == 0 else And(core_checks, other_checks)

should become

return And(core_checks, identity_element if value == 0 else other_checks)

Solution

The compiler should detect identity elements for each operator and simplify them.

Urgency

No urgency at all.

@StylishTriangles
Copy link
Contributor

This would probably require for single argument NaryExpressions to exist, if that's the case it would be great if such change to the API was public. One use case could be And(*[validate(thing) for thing in things]) and wouldn't require a special case for a single element array.

@jasonpaulos
Copy link
Contributor

jasonpaulos commented Sep 8, 2021

Allowing single argument NaryExpressions sounds reasonable and simple to do. edit: #117 adds this

I also agree that it would be nice if PyTeal could intelligently optimize away constants like the original issue suggests. There are many places this could happen, and identify elements in And and Or are one of them.

Another good place would be equality conditions, since it's pretty common to check if a number is equal to 0 or 1. However, this is a redundant check in TEAL, since 0 and 1 can already be used to represent boolean values. For example, the TEAL code <number-to-check>; int 0; == is equivalent to just <number-to-check>; !.

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

No branches or pull requests

4 participants