Boolean logic: "short circuit evaluation"?
Boolean logic: "short circuit evaluation"?
I have just been asked whether the Spectrum is able to perform "short circuit evaluation" of IF... THEN conditions or not. I'd never heard of the term. But apparently, what it means is:
IF (condition 1) OR (condition 2) OR (condition 3)... THEN action
IF condition 1 is true, there's no need to evaluate conditions 2, 3 and so on... just skip to THEN and do what's required, and that's "short circuit evaluatuion".
I assume that the Spectrum is basic enough (in BASIC...) to evaulate all the conditions blindingly and unquestioningly in order before reaching THEN and doing what's required, even if condition 1 was true and ten further conditions were all false and we wouldn't care about those anyway.
What's the truth? Is there a program that could prove it one way or the other, short of getting a debugger and stepping through the ROM processes one by one to see where it's going?
IF (condition 1) OR (condition 2) OR (condition 3)... THEN action
IF condition 1 is true, there's no need to evaluate conditions 2, 3 and so on... just skip to THEN and do what's required, and that's "short circuit evaluatuion".
I assume that the Spectrum is basic enough (in BASIC...) to evaulate all the conditions blindingly and unquestioningly in order before reaching THEN and doing what's required, even if condition 1 was true and ten further conditions were all false and we wouldn't care about those anyway.
What's the truth? Is there a program that could prove it one way or the other, short of getting a debugger and stepping through the ROM processes one by one to see where it's going?
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
Re: Boolean logic: "short circuit evaluation"?
Code: Select all
10 IF NOT USR 82 AND USR 0 THEN PRINT "BOO!"
Re: Boolean logic: "short circuit evaluation"?
That answers that! Because the question came from Germany, you've earned yourself a Münchener Weißwurst and Sauerkraut sandwich. Enjoy!
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
- 1024MAK
- Bugaboo
- Posts: 3123
- Joined: Wed Nov 15, 2017 2:52 pm
- Location: Sunny Somerset in the U.K. in Europe
Re: Boolean logic: "short circuit evaluation"?
Standby alert
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
Re: Boolean logic: "short circuit evaluation"?
If I've understand it should be something as (for AND)
and for OR
?
Code: Select all
10 IF a THEN IF b THEN IF c THEN PRINT "oki doki"
Code: Select all
10 IF NOT a THEN IF NOT b THEN IF NOT c THEN GOTO 30
20 PRINT "oki doki"
30 STOP
Re: Boolean logic: "short circuit evaluation"?
Annoyingly, I can't find it now, but somebody (I thought it was Philip Kendall, shadowmagic) had a clever BASIC hack for writing a recursive DEF FN which would eventually terminate in a base case. Since Spectrum BASIC does not usually short-circuit, but evaluates both sides, it used VAL "..." with a string expression to get around this.
Anyone know where this is? It seems semi-relevant.
Anyone know where this is? It seems semi-relevant.
Re: Boolean logic: "short circuit evaluation"?
That’s correct. Put the expression most likely to fail first, then the others do not need to be evaluated at all. This IS short-circuit evaluation. Changing this introduces bugs in other languages when the expressions can have side-effects (e,g. changing global state from within a function) but (apart from undocumented SysVar state) this doesn’t apply to any ZX BASIC.+3code wrote: ↑Thu Dec 02, 2021 6:42 pm If I've understand it should be something as (for AND)and for ORCode: Select all
10 IF a THEN IF b THEN IF c THEN PRINT "oki doki"
?Code: Select all
10 IF NOT a THEN IF NOT b THEN IF NOT c THEN GOTO 30 20 PRINT "oki doki" 30 STOP
Re: Boolean logic: "short circuit evaluation"?
It has an example on Worldofspectrum, I will take a look since I don't want to built one again myself.equinox wrote: ↑Thu Dec 02, 2021 9:06 pm Annoyingly, I can't find it now, but somebody (I thought it was Philip Kendall, shadowmagic) had a clever BASIC hack for writing a recursive DEF FN which would eventually terminate in a base case. Since Spectrum BASIC does not usually short-circuit, but evaluates both sides, it used VAL "..." with a string expression to get around this.
Anyone know where this is? It seems semi-relevant.
Re: Boolean logic: "short circuit evaluation"?
also entioned in the link i postedequinox wrote: ↑Thu Dec 02, 2021 9:06 pm Annoyingly, I can't find it now, but somebody (I thought it was Philip Kendall, shadowmagic) had a clever BASIC hack for writing a recursive DEF FN which would eventually terminate in a base case. Since Spectrum BASIC does not usually short-circuit, but evaluates both sides, it used VAL "..." with a string expression to get around this.
Anyone know where this is? It seems semi-relevant.
https://www.shadowmagic.org.uk/spectrum/recursive.html
- ParadigmShifter
- Manic Miner
- Posts: 671
- Joined: Sat Sep 09, 2023 4:55 am
Re: Boolean logic: "short circuit evaluation"?
It's slow though and the reason you would want to use lazy evaluation is as an optimisation?
Just split the IF statement into pieces and skip the latter part if the first test fails?
That said lazy evaluation would of course have been better, but they didn't implement it so bad luck.
EDIT: And of course I messed up the logic before this edit lol... was doing the OR case not the AND one
EDIT2: Using recursion for a factorial function is also not a good example of how to write code either tbh. Just use a loop or a lookup table or a memo (I think that is the right term for it, might be memoisation though - where you cache values in an array each time you work out a new one so if you call it a lot you end up doing less work).
EDIT3: I see they turn a sensible loop based version of factorial into that monstrosity though And they don't check for non-integer or negative values of n. They also don't seem to know that 0! = 1 (0! = 1 because n! is the number of ways to arrange n objects when order is important... there's exactly 1 way to order 0 objects in order). I won't go into why non-integer values are bad unless you want to implement the gamma function lol.
EDIT4: I see they do return 1 for 0! in the FOR loop version (and also any other negative input). And it rounds down if you pass a positive non-integer (should probably return some error value like -1 or 0 though). Should return Gamma(n+1) = ((n+1)-1)! really though of course
Just split the IF statement into pieces and skip the latter part if the first test fails?
Code: Select all
10 IF a=1 AND b=1 THEN PRINT "true"
turn that into
10 IF a<>1 THEN GOTO 30
20 IF b=1 THEN PRINT "true"
30 REM false
EDIT: And of course I messed up the logic before this edit lol... was doing the OR case not the AND one
EDIT2: Using recursion for a factorial function is also not a good example of how to write code either tbh. Just use a loop or a lookup table or a memo (I think that is the right term for it, might be memoisation though - where you cache values in an array each time you work out a new one so if you call it a lot you end up doing less work).
EDIT3: I see they turn a sensible loop based version of factorial into that monstrosity though And they don't check for non-integer or negative values of n. They also don't seem to know that 0! = 1 (0! = 1 because n! is the number of ways to arrange n objects when order is important... there's exactly 1 way to order 0 objects in order). I won't go into why non-integer values are bad unless you want to implement the gamma function lol.
EDIT4: I see they do return 1 for 0! in the FOR loop version (and also any other negative input). And it rounds down if you pass a positive non-integer (should probably return some error value like -1 or 0 though). Should return Gamma(n+1) = ((n+1)-1)! really though of course
Last edited by ParadigmShifter on Sun Dec 24, 2023 11:01 am, edited 2 times in total.
- 1024MAK
- Bugaboo
- Posts: 3123
- Joined: Wed Nov 15, 2017 2:52 pm
- Location: Sunny Somerset in the U.K. in Europe
Re: Boolean logic: "short circuit evaluation"?
At the end of the day, the fastest way to make a decision is not to actually make a decision, but to just pretend that you have...!
10 PRINT "Number guess"
20 PRINT "Please think of a number between 1 and 10"
30 PRINT "Then press ENTER"
40 PRINT "Will then guess what number I think you chose"
50 INPUT A$
60 PRINT "Okay, thinking about it..."
79 FOR A=0 TO 150:NEXT A
80 PRINT "Having thought about it,"
90 PRINT "was your number ";INT(1+RND*10);"?"
Mark
10 PRINT "Number guess"
20 PRINT "Please think of a number between 1 and 10"
30 PRINT "Then press ENTER"
40 PRINT "Will then guess what number I think you chose"
50 INPUT A$
60 PRINT "Okay, thinking about it..."
79 FOR A=0 TO 150:NEXT A
80 PRINT "Having thought about it,"
90 PRINT "was your number ";INT(1+RND*10);"?"
Mark
Standby alert
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
- ParadigmShifter
- Manic Miner
- Posts: 671
- Joined: Sat Sep 09, 2023 4:55 am
Re: Boolean logic: "short circuit evaluation"?
Some truth to that... fastest code is code you don't execute, I think Knuth said that at some point. I can't remember what he said was the greatest waste of machine cycles ever, may have been Rogue? - was definitely a game anyway
Re: Boolean logic: "short circuit evaluation"?
You can try the program below if you prefer not reset your machine.ketmar wrote: ↑Thu Dec 02, 2021 12:30 pm"USR 82" is just a ret, and returns 82. if short circuit is on, then nothing will happen. otherwise the Speccy will reset.Code: Select all
10 IF NOT USR 82 AND USR 0 THEN PRINT "BOO!"
Code: Select all
10 IF 1 OR 2/0 THEN PRINT "OK"
- ParadigmShifter
- Manic Miner
- Posts: 671
- Joined: Sat Sep 09, 2023 4:55 am
Re: Boolean logic: "short circuit evaluation"?
Oof... number too big.
There's one thing that 2/0 definitely isn't, and that's a number
I guess they didn't have space for another error message just for division by 0.
EDIT: On the other hand PRINT LN 0 gives "invalid argument" so they could have used that (although there's 2 arguments for / obvs).
There's one thing that 2/0 definitely isn't, and that's a number
I guess they didn't have space for another error message just for division by 0.
EDIT: On the other hand PRINT LN 0 gives "invalid argument" so they could have used that (although there's 2 arguments for / obvs).
Re: Boolean logic: "short circuit evaluation"?
I wanted to quote "New Functions for Sinclair Basic" (by Andrew Owen, a.k.a. chev or cheveron)
but I can't find it! I can't find it anywhere online, to link to -- although I have a personal copy.
...Should I post it?
but I can't find it! I can't find it anywhere online, to link to -- although I have a personal copy.
...Should I post it?
Re: Boolean logic: "short circuit evaluation"?
I'd tread carefully there - best to check with the man himself first.