Boolean logic: "short circuit evaluation"?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
TMD2003
Rick Dangerous
Posts: 2045
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Boolean logic: "short circuit evaluation"?

Post by TMD2003 »

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?
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
User avatar
ketmar
Manic Miner
Posts: 713
Joined: Tue Jun 16, 2020 5:25 pm
Location: Ukraine

Re: Boolean logic: "short circuit evaluation"?

Post by ketmar »

Code: Select all

10 IF NOT USR 82 AND USR 0 THEN PRINT "BOO!"
"USR 82" is just a ret, and returns 82. if short circuit is on, then nothing will happen. otherwise the Speccy will reset. ;-)
User avatar
TMD2003
Rick Dangerous
Posts: 2045
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: Boolean logic: "short circuit evaluation"?

Post by TMD2003 »

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!
User avatar
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"?

Post by 1024MAK »

TMD2003 wrote: Thu Dec 02, 2021 12:19 pm 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.
Maybe the person that asked had read this can of worms…

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
+3code

Re: Boolean logic: "short circuit evaluation"?

Post by +3code »

If I've understand it should be something as (for AND)

Code: Select all

10 IF a THEN IF b THEN IF c THEN PRINT "oki doki"
and for OR

Code: Select all

10 IF NOT a THEN IF NOT b THEN IF NOT c THEN GOTO 30
20 PRINT "oki doki"
30 STOP
?
equinox
Dynamite Dan
Posts: 1052
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Re: Boolean logic: "short circuit evaluation"?

Post by equinox »

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.
SNG
Drutt
Posts: 13
Joined: Sat Dec 23, 2023 12:16 am

Re: Boolean logic: "short circuit evaluation"?

Post by SNG »

+3code wrote: Thu Dec 02, 2021 6:42 pm If I've understand it should be something as (for AND)

Code: Select all

10 IF a THEN IF b THEN IF c THEN PRINT "oki doki"
and for OR

Code: Select all

10 IF NOT a THEN IF NOT b THEN IF NOT c THEN GOTO 30
20 PRINT "oki doki"
30 STOP
?
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.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: Boolean logic: "short circuit evaluation"?

Post by Dr beep »

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.
It has an example on Worldofspectrum, I will take a look since I don't want to built one again myself.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: Boolean logic: "short circuit evaluation"?

Post by Dr beep »

Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: Boolean logic: "short circuit evaluation"?

Post by Dr beep »

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.
also entioned in the link i posted

https://www.shadowmagic.org.uk/spectrum/recursive.html
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Boolean logic: "short circuit evaluation"?

Post by ParadigmShifter »

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?

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
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 :roll: (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.
User avatar
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"?

Post by 1024MAK »

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
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Boolean logic: "short circuit evaluation"?

Post by ParadigmShifter »

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 :)
User avatar
tkfan
Drutt
Posts: 28
Joined: Wed Jan 05, 2022 4:35 am
Location: Curitiba - Brazil
Contact:

Re: Boolean logic: "short circuit evaluation"?

Post by tkfan »

ketmar wrote: Thu Dec 02, 2021 12:30 pm

Code: Select all

10 IF NOT USR 82 AND USR 0 THEN PRINT "BOO!"
"USR 82" is just a ret, and returns 82. if short circuit is on, then nothing will happen. otherwise the Speccy will reset. ;-)
You can try the program below if you prefer not reset your machine. :-)

Code: Select all

10 IF 1 OR 2/0 THEN PRINT "OK"
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Boolean logic: "short circuit evaluation"?

Post by ParadigmShifter »

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).
equinox
Dynamite Dan
Posts: 1052
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Re: Boolean logic: "short circuit evaluation"?

Post by equinox »

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?
User avatar
R-Tape
Site Admin
Posts: 6409
Joined: Thu Nov 09, 2017 11:46 am

Re: Boolean logic: "short circuit evaluation"?

Post by R-Tape »

equinox wrote: Thu Dec 28, 2023 12:30 amalthough I have a personal copy.
...Should I post it?
I'd tread carefully there - best to check with the man himself first.
RST#08
Drutt
Posts: 30
Joined: Sun Jul 24, 2022 6:29 pm

Re: Boolean logic: "short circuit evaluation"?

Post by RST#08 »

equinox wrote: Thu Dec 28, 2023 12:30 am I can't find it anywhere online, to link to
Is this the one?
https://worldofspectrum.org/forums/discussion/6093/
Post Reply