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

Short circuit evaluation for logic operators #591

Closed
anton-k opened this issue Feb 2, 2016 · 8 comments
Closed

Short circuit evaluation for logic operators #591

anton-k opened this issue Feb 2, 2016 · 8 comments

Comments

@anton-k
Copy link

anton-k commented Feb 2, 2016

This feature exists in many languages. It would be nice to have it in Csound. So that code below doesn't produce a Warning:

if (1 == 1 ||  1/0 == 0) then
endif

This is a contrived example. But it's often useful to have a conditional statement that relies on previous conditional statements in the chain of OR's.

@kunstmusik
Copy link
Member

Just adding a note, I was thinking whether this would be backwards compatible to implement and I think it is. We don't currently expose the boolean types (b and B) to users to define variables and I'm almost certain that all opcodes that process booleans are all stateless. I think the code generation would change from something like:

#b0 == 1, 1
#i0  div 1, 0
#b1 == #i0, 0
#b2 || #b0, #b1
cngoto #b2, #label0 
...code
#label0 

to:

#b0 == 1, 1
cngoto #b0, #label0
#i0  div 1, 0
#b1 == #i0, 0
cngoto #b1, #label0 
...code
#label0 

@vlazzarini
Copy link
Member

Do you mean

#b2 or #b0, #b1

So you're not doing an explicit OR in this case?

@kunstmusik
Copy link
Member

Yes sorry, should have been an or instead of and. I've modified the example code to show the real opcodes as reported from using --verbose when compiling:

if (1 == 0 || 1 == 1) then
...
endif

I think the new behavior would explicitly not do a call to ||.

@jpffitch
Copy link
Contributor

jpffitch commented Feb 9, 2016

Just to note that this is not a compatible change. In te proposed short
cut the valus of
if (k=0 || (k1 == fn())
where fn as internal state. If short cut then that state will not change.

I suspect this is unlikely to occur but it is not without cange.

==John

On Tue, 9 Feb 2016, Steven Yi wrote:

Yes sorry, should have been an or instead of and. I've modified the example
code to show the real opcodes as reported from using --verbose when compiling:

if (1 == 0 || 1 == 1) then
...
endif


Reply to this email directly or view it on
GitHub.[AELNQlnCqDrnWtNEjJRhE_MxXTk2qZ6Gks5pigYEgaJpZM4HRYEg.gif]

@kunstmusik
Copy link
Member

Thanks @jpffitch for that pointing that out, I was too quick in my analysis regarding stateful opcodes. I'm struggling to imagine a scenario where one would use this kind of code, but it's certainly possible. Regardless of what we do, we should probably update documentation with the exact behavior.

@anton-k anton-k changed the title Shirt circuit evaluation for logic operators Short circuit evaluation for logic operators Feb 11, 2016
@kunstmusik
Copy link
Member

@jpffitch , @vlazzarini , @anton-k : I'm going through issues and wondering if this is a "won't fix" since it's not backwards compatible. Thoughts? (Assigning to 6.08 for the timebeing.)

@kunstmusik kunstmusik added this to the 6.08 milestone Aug 24, 2016
@jpffitch
Copy link
Contributor

jpffitch commented Aug 28, 2016 via email

@kunstmusik
Copy link
Member

Closing as backwards compatibility would be broken.

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

No branches or pull requests

4 participants