Jump to content

Short-circuit evaluation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Bitwise operators vs. Boolean operators
Tag: nowiki added
 
(47 intermediate revisions by 33 users not shown)
Line 1: Line 1:
{{Short description|Programming language construct}}
{{distinguish|Short-circuit test}}
{{Distinguish|Short-circuit test}}
{{Refimprove|date=August 2013}}
{{More citations needed|date=August 2013}}
{{Programming evaluation}}
{{Evaluation strategy}}
'''Short-circuit evaluation''', '''minimal evaluation''', or '''McCarthy evaluation''' (after [[John McCarthy (computer scientist)|John McCarthy]]) is the semantics of some [[Logical connective|Boolean operators]] in some [[programming language]]s in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the <code>AND</code> function evaluates to <code>false</code>, the overall value must be <code>false</code>; and when the first argument of the <code>OR</code> function evaluates to <code>true</code>, the overall value must be <code>true</code>.
'''Short-circuit evaluation''', '''minimal evaluation''', or '''McCarthy evaluation''' (after [[John McCarthy (computer scientist)|John McCarthy]]) is the semantics of some [[Logical connective|Boolean operators]] in some [[programming language]]s in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the <code>AND</code> function evaluates to <code>false</code>, the overall value must be <code>false</code>; and when the first argument of the <code>OR</code> function evaluates to <code>true</code>, the overall value must be <code>true</code>.


In programming languages with [[Lazy evaluation|lazy evaluation]] ([[Lisp (programming language)|Lisp]], [[Perl]], [[Haskell (programming language)|Haskell]]), the usual Boolean operators are short-circuit. In others ([[Ada (programming language)|Ada]], [[Java (programming language)|Java]], [[Delphi (programming language)|Delphi]]), both short-circuit and standard Boolean operators are available. For some Boolean operations, like ''[[exclusive or]]'' (XOR), it is not possible to short-circuit, because both operands are always required to determine the result.
In programming languages with [[lazy evaluation]] ([[Lisp (programming language)|Lisp]], [[Perl]], [[Haskell]]), the usual [[Boolean expression|Boolean operators]] short-circuit. In others ([[Ada (programming language)|Ada]], [[Java (programming language)|Java]], [[Delphi (software)|Delphi]]), both short-circuit and standard Boolean operators are available. For some Boolean operations, like ''[[exclusive or]]'' (XOR), it is impossible to short-circuit, because both operands are always needed to determine a result.


Short-circuit operators are, in effect, [[control structure]]s rather than simple arithmetic operators, as they are not [[strict function|strict]]. In [[imperative language]] terms (notably [[C (programming language)|C]] and [[C++]]), where side effects are important, short-circuit operators introduce a [[sequence point]] they completely evaluate the first argument, including any [[Side effect (computer science)|side effects]], before (optionally) processing the second argument. [[ALGOL 68]] used ''proceduring'' to achieve ''user-defined'' short-circuit operators and procedures.
Short-circuit operators are, in effect, [[control structure]]s rather than simple arithmetic operators, as they are not [[Strict function|strict]]. In [[imperative language]] terms (notably [[C (programming language)|C]] and [[C++]]), where side effects are important, short-circuit operators introduce a [[sequence point]]: they completely evaluate the first argument, including any [[Side effect (computer science)|side effects]], before (optionally) processing the second argument. [[ALGOL 68]] used ''proceduring'' to achieve ''user-defined'' short-circuit operators and procedures.


The use of short-circuit operators has been criticized as problematic:
The use of short-circuit operators has been criticized as problematic:
{{Quote
{{Quote
|text = The conditional connectives — "<u>cand</u>" and "<u>cor</u>" for short — are ... less innocent than they might seem at first sight. For instance, <u>cor</u> does not distribute over <u>cand</u>: compare
|text = The conditional connectives — "<u>cand</u>" and "<u>cor</u>" for short — are ... less innocent than they might seem at first sight. For instance, <u>cor</u> does not distribute over <u>cand</u>: compare
:(A <u>cand</u> B) <u>cor</u> C ''with'' (A <u>cor</u> C) <u>cand</u> (B <u>cor</u> C);
:(A <u>cand</u> B) <u>cor</u> C ''with'' (A <u>cor</u> C) <u>cand</u> (B <u>cor</u> C);
in the case ¬A ∧ C , the second expression requires B to be defined, the first one does not. Because the conditional connectives thus complicate the formal reasoning about programs, they are better avoided.
in the case ¬A ∧ C , the second expression requires B to be defined, the first one does not. Because the conditional connectives thus complicate the formal reasoning about programs, they are better avoided.
|author = [[Edsger W. Dijkstra]]<ref> [[Edsger W. Dijkstra]] "On a somewhat disappointing correspondence", EWD1009-0, 25 May 1987 [http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1009.PDF full text]</ref>}}
|author = [[Edsger W. Dijkstra]]<ref>[[Edsger W. Dijkstra]] "On a somewhat disappointing correspondence", EWD1009-0, 25 May 1987 [http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1009.PDF full text]</ref>}}


== Definition ==
== Definition ==
Line 21: Line 22:


=== Precedence ===
=== Precedence ===
Although {{code|AND}} takes [[operator precedence|precedence]] over {{code|OR}} in many languages, this is not a universal property of short-circuit evaluation. An example of the two operator taking the same precedence and being [[left-associative]] with each other is [[POSIX shell]]'s command-list syntax.<ref>{{cite web |title=Shell Command Language |url=https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |website=pubs.opengroup.org}}</ref>{{rp|at=&sect;2.9.3}}
Although {{code|AND}} takes [[operator precedence|precedence]] over {{code|OR}} in many languages, this is not a universal property of short-circuit evaluation. An example of the two operators taking the same precedence and being [[left-associative]] with each other is [[POSIX shell]]'s command-list syntax.<ref>{{cite web |title=Shell Command Language |url=https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |website=pubs.opengroup.org}}</ref>{{rp|at=§2.9.3}}


The following simple left-to-right evaluator enforces a precedence of {{code|AND}} over {{code|OR}} by a {{code|continue}}:
The following simple left-to-right evaluator enforces a precedence of {{code|AND}} over {{code|OR}} by a {{code|continue}}:


'''function''' short-circuit-eval (''operators'', ''values'')
'''function''' short-circuit-eval (''operators'', ''values'')
'''let''' ''result'' := True
'''let''' ''result'' := True
'''for each''' (''op'', ''val'') in (''operators'', ''values''):
'''for each''' (''op'', ''val'') in (''operators'', ''values''):
'''if''' ''op'' = "AND" && ''result'' = False
'''if''' ''op'' = "AND" && ''result'' = False
'''continue'''
'''continue'''
'''else if''' ''op'' = "OR" && ''result'' = True
'''else if''' ''op'' = "OR" && ''result'' = True
'''return''' ''result''
'''return''' ''result''
'''else'''
'''else'''
''result'' := ''val''
''result'' := ''val''
'''return''' ''result''
'''return''' ''result''


=== Formalization ===
=== Formalization ===
Short-circuit logic, with or without side-effects, have been formalized based on [[Hoare logic#Conditional_rule|Hoare's conditional]]. A result is that non-short-circuiting operators can be defined out of short-circuit logic to have the same sequence of evalulation.<ref>{{cite arXiv |authors=Jan A. Bergstra, A. Ponse, D.J.C. Staudt |date=2010 |title=Short-circuit logic |eprint=1010.3674|class=cs.LO}}</ref>
Short-circuit logic, with or without side-effects, have been formalized based on [[Hoare logic#Conditional rule|Hoare's conditional]]. A result is that non-short-circuiting operators can be defined out of short-circuit logic to have the same sequence of evaluation.<ref>{{cite arXiv |last1=Bergstra |first1=Jan A. |last2=Ponse |first2=A. |last3=Staudt |first3=D.J.C. |date=2010 |title=Short-circuit logic |eprint=1010.3674|class=cs.LO}}</ref>

==Support in common programming and scripting languages==
As you look at the table below, keep in mind that bitwise operators often do not behave exactly like logical operators, even if both arguments are of <code>0</code>, <code>1</code> or Boolean type.

Examples:
* In JavaScript, each of the following 3 expressions evaluates to <code>false</code>:<br /><code><nowiki>(true & true) === (true && true)</nowiki></code>,<br /><code><nowiki>(false | false) === (false || false)</nowiki></code>,<br /><code><nowiki>(1 & 2) === (1 && 2)</nowiki></code>.
* In PHP, each of the following 3 expressions evaluates to <code>false</code>:<br /><code><nowiki>(true & true) === (true && true)</nowiki></code>,<br /><code><nowiki>(0 | 0) === (0 || 0)</nowiki></code>,<br /><code><nowiki>(1 & 2) === (1 && 2)</nowiki></code>.


==Support in common programming languages==
{| class="wikitable"
{| class="wikitable"
|+ Boolean operators in various languages
|+ Boolean operators in various languages
Line 47: Line 54:
| ''none''
| ''none''
| <code>and</code>, <code>or</code>
| <code>and</code>, <code>or</code>
| Boolean{{efn |name=abap-apl |1=ABAP and APL have no distinct boolean type.}}
| Boolean<sup>1</sup>
|-
|-
| [[Ada (programming language)|Ada]]
| [[Ada (programming language)|Ada]]
Line 62: Line 69:
| <code>∧</code>, <code>∨</code>, <code>⍲</code> (nand), <code>⍱</code> (nor), etc.
| <code>∧</code>, <code>∨</code>, <code>⍲</code> (nand), <code>⍱</code> (nor), etc.
| <code>:AndIf</code>, <code>:OrIf</code>
| <code>:AndIf</code>, <code>:OrIf</code>
| Boolean<sup>1</sup>
| Boolean{{efn |name=abap-apl}}
|-
|-
| [[awk]]
| [[awk]]
| ''none''
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| Boolean
|-
| [[Bash (Unix shell)|Bash]]
| ''none''
| ''none''
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>
Line 70: Line 82:
|-
|-
| [[C (programming language)|C]], [[Objective-C]]
| [[C (programming language)|C]], [[Objective-C]]
| <code>&</code>, <code><nowiki>|</nowiki></code>{{efn |name=bitwise_c |1=The bitwise operators behave like boolean operators when both arguments are of type <code>bool</code> or take only the values <code>0</code> or <code>1</code>.<ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf ISO/IEC 9899 standard, sections 6.2.5, 6.3.1.2, 6.5 and 7.16.]</ref>}}
| <code>&</code>,<code><nowiki>|</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code><ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf ISO/IEC 9899 standard, section 6.5.13]</ref>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code><ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf ISO/IEC 9899 standard, section 6.5.13]</ref>
| int (<code>&&</code>,<code><nowiki>||</nowiki></code>), opnd-dependent (<code><nowiki>?</nowiki></code>)
| int (<code>&</code>, <code><nowiki>|</nowiki></code>, <code>&&</code>,<code><nowiki>||</nowiki></code>), opnd-dependent (<code><nowiki>?</nowiki></code>)
|-
|-
| [[C++]]{{efn |name=cpp |1=When [[operator overloading|overloaded]], the operators <code>&&</code> and <code><nowiki>||</nowiki></code> are eager and can return any type.}}
| [[C++]]<sup>2</sup>
| ''none''
| <code>&</code>,<code><nowiki>|</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code><ref>[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf ISO/IEC IS 14882 draft.]</ref>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code><ref>[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf ISO/IEC IS 14882 draft.]</ref>
| Boolean (<code>&&</code>,<code><nowiki>||</nowiki></code>), opnd-dependent (<code><nowiki>?</nowiki></code>)
| Boolean (<code>&&</code>,<code><nowiki>||</nowiki></code>), opnd-dependent (<code><nowiki>?</nowiki></code>)
Line 89: Line 101:
| Boolean
| Boolean
|-
|-
| [[D (programming language)|D]]{{efn |name=d |1=This only applies to runtime-evaluated expressions, <code>static if</code> and <code>static assert</code>. Expressions in static initializers or manifest constants use eager evaluation.}}
| [[D (programming language)|D]]<sup>3</sup>
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>?</nowiki></code>
Line 104: Line 116:
| Boolean
| Boolean
|-
|-
| [[Fortran]]{{efn |name=fortran |1=Fortran operators are neither short-circuit nor eager: the language specification allows the [[compiler]] to select the method for optimization.}}
| [[Fortran]]<sup>4</sup>
| <code>.and.</code>, <code>.or.</code>
| <code>.and.</code>, <code>.or.</code>
| <code>.and.</code>, <code>.or.</code>
| <code>.and.</code>, <code>.or.</code>
Line 119: Line 131:
| Boolean
| Boolean
|-
|-
| [[JavaScript]], [[Julia (programming language)|Julia]]
| [[JavaScript]]
| ''none''
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&&</code>, <code>&&=</code>, <code><nowiki>||</nowiki></code>, <code><nowiki>||=</nowiki></code>
| Last value
|-
| [[Julia (programming language)|Julia]]
| ''none''
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| Last value
| Last value
Line 155: Line 172:
|-
|-
| [[OCaml]]
| [[OCaml]]
| <code>land</code>, <code>lor</code><ref>{{cite web | url=https://v2.ocaml.org/manual/expr.html#ss:expr-operators | title=OCaml - the OCaml language }}</ref>
| ''none''
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| Boolean
| Boolean
|-
|-
| [[Pascal (programming language)|Pascal]]
| [[Pascal (programming language)|Pascal]]
| <code>and</code>, <code>or</code>{{efn |name=pascal-1 |1=[[Pascal (programming language)#ISO/IEC 10206:1990 Extended Pascal|ISO/IEC 10206:1990 Extended Pascal]] allows, but does not require, short-circuiting.}}{{efn |name=delphi |1=[[Delphi (software)|Delphi]] and [[Free Pascal]] default to short circuit evaluation. This may be changed by [[compiler]] options but does not seem to be used widely.}}
| <code>and</code>, <code>or</code><sup>5</sup>,<sup>9</sup>
| <code>and_then</code>, <code>or_else</code><!--{{refn |group=lower-alpha |name=pascal-2 |1=ISO/IEC 10206:1990 Extended Pascal supports <code>and_then</code> and <code>or_else</code>.<ref>{{cite web|url=http://www.gnu-pascal.de/gpc/and_005fthen.html#and_005fthen#GNU |title=and_then - The GNU Pascal Manual |publisher=Gnu-pascal.de |access-date=2013-08-24}}</ref>}}-->{{efn |name=delphi}}
| <code>and_then</code>, <code>or_else</code><sup>6</sup>,<sup>9</sup>
| Boolean
| Boolean
|-
|-
| [[Perl]], [[Ruby (programming language)|Ruby]]
| [[Perl]]
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&&</code>, <code>and</code>, <code><nowiki>||</nowiki></code>, <code>or</code>
| <code>&&</code>, <code>and</code>, <code><nowiki>||</nowiki></code>, <code>or</code>
Line 170: Line 187:
|-
|-
| [[PHP]]
| [[PHP]]
| ''none''
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&&</code>, <code>and</code>, <code><nowiki>||</nowiki></code>, <code>or</code>
| <code>&&</code>, <code>and</code>, <code><nowiki>||</nowiki></code>, <code>or</code>
| Boolean
| Boolean
Line 178: Line 195:
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code>
| Last value (exit)
| Last value (exit)
|-
| [[PowerShell]] Scripting Language
| ''none''
| <code>-and</code>, <code>-or</code>
| Boolean
|-
|-
| [[Python (programming language)|Python]]
| [[Python (programming language)|Python]]
| <code>&</code>, <code><nowiki>|</nowiki></code>
| ''none''<ref>https://wiki.python.org/moin/BitwiseOperators</ref>
| <code>and</code>, <code>or</code>
| <code>and</code>, <code>or</code>
| Last value
| Last value
|-
|-
|[[Rust (programming language)|Rust]]
| [[Ruby (programming language)|Ruby]]
|<code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&</code>, <code><nowiki>|</nowiki></code>
|<code>&&</code>, <code><nowiki>||</nowiki></code><ref>{{Cite web|url=https://doc.rust-lang.org/std/ops/index.html|title=std::ops - Rust|website=doc.rust-lang.org|access-date=2019-02-12}}</ref>
| <code>&&</code>, <code>and</code>, <code><nowiki>||</nowiki></code>, <code>or</code><ref>{{Cite web |title=operators - Documentation for Ruby 3.3 |url=https://docs.ruby-lang.org/en/3.3/syntax/operators_rdoc.html#label-Logical+Operators |access-date=2024-04-02 |website=docs.ruby-lang.org}}</ref>
| Last value
|Boolean
|-
| [[Rust (programming language)|Rust]]
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&&</code>, <code><nowiki>||</nowiki></code><ref>{{Cite web|url=https://doc.rust-lang.org/std/ops/index.html|title=std::ops - Rust|website=doc.rust-lang.org|access-date=2019-02-12}}</ref>
| Boolean
|-
|-
| [[Smalltalk]]
| [[Smalltalk]]
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>and:</code>, <code>or:</code>{{efn |name=smalltalk |1=Smalltalk uses short-circuit semantics as long as the argument to <code>and:</code> is a block (e.g., <code>false and: [Transcript show: 'Wont see me']</code>).}}
| <code>and:</code>, <code>or:</code><sup>7</sup>
| Boolean
| Boolean
|-
|-
| [[Standard ML]]
| [[Standard ML]]
| {{unk}}
| {{Unknown}}
| <code>andalso</code>, <code>orelse</code>
| <code>andalso</code>, <code>orelse</code>
| Boolean
| Boolean
Line 203: Line 230:
| <code>and</code>, <code>or</code><ref>[https://www.etsi.org/deliver/etsi_es/201800_201899/20187301/04.10.01_60/es_20187301v041001p.pdf ETSI ES 201 873-1 V4.10.1, section 7.1.4]</ref>
| <code>and</code>, <code>or</code><ref>[https://www.etsi.org/deliver/etsi_es/201800_201899/20187301/04.10.01_60/es_20187301v041001p.pdf ETSI ES 201 873-1 V4.10.1, section 7.1.4]</ref>
| Boolean
| Boolean
|-
|Beckhoff TwinCAT® ([[IEC 61131-3]]){{efn |name=twincat |1=The norm [[IEC 61131-3]] doesn't actually define if <code>AND</code> and <code>OR</code> use short-circuit evaluation and it doesn't define the operators <code>AND_THEN</code> and <code>OR_ELSE</code>. The entries in the table show how it works for Beckhoff TwinCAT®.}}
|<code>AND</code>, <code>OR</code>
|<code>AND_THEN</code>,<ref>{{Cite web|title=Beckhoff Information System - English|url=https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2528923787.html&id=|access-date=2021-08-16|website=infosys.beckhoff.com}}</ref> <code>OR_ELSE</code><ref>{{Cite web|title=Beckhoff Information System - English|url=https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2528923787.html&id=|access-date=2021-08-16|website=infosys.beckhoff.com}}</ref>
|Boolean
|-
|-
| [[Visual Basic .NET]]
| [[Visual Basic .NET]]
Line 211: Line 243:
| [[Visual Basic]], [[Visual Basic for Applications]] (VBA)
| [[Visual Basic]], [[Visual Basic for Applications]] (VBA)
| <code>And</code>, <code>Or</code>
| <code>And</code>, <code>Or</code>
| <code>Select Case</code>{{efn |name=basic |1=[[BASIC]] languages that supported CASE statements did so by using the conditional evaluation system, rather than as jump tables limited to fixed labels.}}
| <code>Select Case</code><sup>8</sup>
| Numeric
| Numeric
|-
|-
Line 221: Line 253:
| ZTT
| ZTT
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <code>&</code>, <code><nowiki>|</nowiki></code>
| <i>none</i>
| ''none''
| Boolean
| Boolean
|}
|}
{{notelist}}
<sup>1</sup> ABAP and APL have no distinct boolean type.<br/>
<sup>2</sup> When overloaded, the operators <code>&&</code> and <code>||</code> are eager and can return any type.<br/>
<sup>3</sup> This only applies to runtime-evaluated expressions, <code>static if</code> and <code>static assert</code>. Expressions in static initializers or manifest constants use eager evaluation.<br>
<sup>4</sup> Fortran operators are neither short-circuit nor eager: the language specification allows the compiler to select the method for optimization.<br/>
<sup>5</sup> [[Pascal (programming language)#ISO/IEC 10206:1990 Extended Pascal|ISO/IEC 10206:1990 Extended Pascal]] allows, but does not require, short-circuiting.<br/>
<sup>6</sup> ISO/IEC 10206:1990 Extended Pascal supports <code>and_then</code> and <code>or_else</code>.<ref>{{cite web|url=http://www.gnu-pascal.de/gpc/and_005fthen.html#and_005fthen#GNU |title=and_then - The GNU Pascal Manual |publisher=Gnu-pascal.de |date= |accessdate=2013-08-24}}</ref><br>
<sup>7</sup> Smalltalk uses short-circuit semantics as long as the argument to <code>and:</code> is a block (e.g., <code>false and: [Transcript show: 'Wont see me']</code>).<br>
<sup>8</sup> [[BASIC]] languages that supported CASE statements did so by using the conditional evaluation system, rather than as jump tables limited to fixed labels.<br>
<sup>9</sup> [[Delphi (programming language)|Delphi]] and [[Free Pascal]] default to short circuit evaluation. This may be changed by compiler options but does not seem to be used widely.<br>


==Common use==
==Common use==

===Avoiding undesired side effects of the second argument===
===Avoiding undesired side effects of the second argument===
Usual example, using a [[C (programming language)|C-based]] language:
Usual example, using a [[C (programming language)|C-based]] language:
Line 255: Line 278:
</syntaxhighlight>
</syntaxhighlight>


In this example, short-circuit evaluation guarantees that <code>myfunc(b)</code> is never called. This is because <code>a != 0</code> evaluates to ''false''. This feature permits two useful programming constructs.
In this example, short-circuit evaluation guarantees that <code>myfunc(b)</code> is never called. This is because <code>a != 0</code> evaluates to ''false''. This feature permits two useful programming constructs.


# If the first sub-expression checks whether an expensive computation is needed and the check evaluates to ''false'', one can eliminate expensive computation in the second argument.
# If the first sub-expression checks whether an expensive computation is needed and the check evaluates to ''false'', one can eliminate expensive computation in the second argument.
# It permits a construct where the first expression guarantees a condition without which the second expression may cause a [[run-time error]].
# It permits a construct where the first expression guarantees a condition without which the second expression may cause a [[run-time error]].


Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches:
Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches:
Line 274: Line 297:


===Idiomatic conditional construct===
===Idiomatic conditional construct===
Since minimal evaluation is part of an operator's semantic definition and not an optional [[compiler optimization|optimization]], a number of coding idioms rely on it as a succinct conditional construct. Examples include:

Since minimal evaluation is part of an operator's semantic definition and not an (optional) optimization, many coding patterns{{Which|date=July 2010}} have come to rely on it as a succinct (if idiomatic) conditional construct. Examples include:


[[Perl]] idioms:
[[Perl]] idioms:
Line 283: Line 305:
</syntaxhighlight>
</syntaxhighlight>


[[POSIX shell]] idioms:<ref>{{cite web |url=https://unix.stackexchange.com/questions/190543/what-does-mean-in-bash |title=What does &#x7c;&#x7c; mean in bash? |publisher=stackexchange.com |accessdate=2019-01-09}}</ref>
[[POSIX shell]] idioms:<ref>{{cite web |url=https://unix.stackexchange.com/questions/190543/what-does-mean-in-bash |title=What does {{!}}{{!}} mean in bash? |publisher=stackexchange.com |access-date=2019-01-09}}</ref>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
modprobe -q some_module && echo "some_module installed" || echo "some_module not installed"
modprobe -q some_module && echo "some_module installed" || echo "some_module not installed"
</syntaxhighlight>
</syntaxhighlight>
This idiom presumes that <code>echo</code> cannot fail.


==Possible problems==
==Possible problems==

=== Untested second condition leads to unperformed side effect ===
=== Untested second condition leads to unperformed side effect ===
Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code
Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code
Line 299: Line 321:
if <code>myfunc(b)</code> is supposed to perform some required operation regardless of whether <code>do_something()</code> is executed, such as allocating system resources, and <code>expressionA</code> evaluates as false, then <code>myfunc(b)</code> will not execute, which could cause problems. Some programming languages, such as [[Java (programming language)|Java]], have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.
if <code>myfunc(b)</code> is supposed to perform some required operation regardless of whether <code>do_something()</code> is executed, such as allocating system resources, and <code>expressionA</code> evaluates as false, then <code>myfunc(b)</code> will not execute, which could cause problems. Some programming languages, such as [[Java (programming language)|Java]], have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.


Problems with unperformed side effect statements can be easily solved with proper programming style, i.e., not using side effects in boolean statements, as using values with side effects in evaluations tends to generally make the code opaque and error-prone.<ref>{{cite web |url=http://www.itu.dk/people/sestoft/papers/SondergaardSestoft1990.pdf |title=Referential Transparency, Definiteness and Unfoldability |publisher=Itu.dk |accessdate=2013-08-24}}</ref>
Problems with unperformed side effect statements can be easily solved with proper programming style, i.e., not using side effects in boolean statements, as using values with side effects in evaluations tends to generally make the code opaque and error-prone.<ref>{{cite web |url=http://www.itu.dk/people/sestoft/papers/SondergaardSestoft1990.pdf |title=Referential Transparency, Definiteness and Unfoldability |publisher=Itu.dk |access-date=2013-08-24}}</ref>


===Reduced efficiency due to constraining optimizations===
===Reduced efficiency due to constraining optimizations===
Short-circuiting can lead to errors in [[branch prediction]] on modern [[central processing unit]]s (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in [[Ray tracing (physics)|ray tracing]].{{clarify|date=November 2010}} Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.{{citation needed|date=October 2016}}


An example of a compiler unable to optimize for such a case is [[Java (programming language)|Java]]'s [[HotSpot (virtual machine)|Hotspot virtual machine]] (VM) as of 2012.<ref>{{cite web |last=Wasserman |first=Louis |date=11 July 2012 |title=Java: What are the cases in which it is better to use unconditional AND (& instead of &&) |url=https://stackoverflow.com/a/11412121 |website=Stack Overflow}}</ref>
Short-circuiting can lead to errors in [[branch prediction]] on modern [[central processing unit]]s (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in [[Ray tracing (physics)|ray tracing]].{{clarify|date=November 2010}} Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.{{cn|date=October 2016}}

An example of a compiler unable to optimize for such a case is [[Java]]'s Hotspot VM as of 2012.<ref>{{cite web |last1=Wasserman |first1=Louis |title=java - What are the cases in which it is better to use unconditional AND (& instead of &&) |url=https://stackoverflow.com/a/11412121 |website=Stack Overflow}}</ref>


==See also==
==See also==
*[[Don't-care condition]]
*[[Don't-care term]]


==References==
==References==
{{Reflist}}
{{Reflist}}

{{DEFAULTSORT:Short-Circuit Evaluation}}
{{John McCarthy}}


[[Category:Articles with example C code]]
[[Category:Articles with example C code]]

Latest revision as of 17:34, 28 April 2024

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.

In programming languages with lazy evaluation (Lisp, Perl, Haskell), the usual Boolean operators short-circuit. In others (Ada, Java, Delphi), both short-circuit and standard Boolean operators are available. For some Boolean operations, like exclusive or (XOR), it is impossible to short-circuit, because both operands are always needed to determine a result.

Short-circuit operators are, in effect, control structures rather than simple arithmetic operators, as they are not strict. In imperative language terms (notably C and C++), where side effects are important, short-circuit operators introduce a sequence point: they completely evaluate the first argument, including any side effects, before (optionally) processing the second argument. ALGOL 68 used proceduring to achieve user-defined short-circuit operators and procedures.

The use of short-circuit operators has been criticized as problematic:

The conditional connectives — "cand" and "cor" for short — are ... less innocent than they might seem at first sight. For instance, cor does not distribute over cand: compare

(A cand B) cor C with (A cor C) cand (B cor C);

in the case ¬A ∧ C , the second expression requires B to be defined, the first one does not. Because the conditional connectives thus complicate the formal reasoning about programs, they are better avoided.

Definition

[edit]

In any programming language that implements short-circuit evaluation, the expression x and y is equivalent to the conditional expression if x then y else x, and the expression x or y is equivalent to if x then x else y. In either case, x is only evaluated once.

The generalized definition above accommodates loosely typed languages that have more than the two truth-values True and False, where short-circuit operators may return the last evaluated subexpression. This is called "last value" in the table below. For a strictly-typed language, the expression is simplified to if x then y else false and if x then true else y respectively for the boolean case.

Precedence

[edit]

Although AND takes precedence over OR in many languages, this is not a universal property of short-circuit evaluation. An example of the two operators taking the same precedence and being left-associative with each other is POSIX shell's command-list syntax.[2]: §2.9.3 

The following simple left-to-right evaluator enforces a precedence of AND over OR by a continue:

function short-circuit-eval (operators, values)
    let result := True
    for each (op, val) in (operators, values):
        if op = "AND" && result = False
            continue
        else if op = "OR" && result = True
            return result
        else
            result := val
    return result

Formalization

[edit]

Short-circuit logic, with or without side-effects, have been formalized based on Hoare's conditional. A result is that non-short-circuiting operators can be defined out of short-circuit logic to have the same sequence of evaluation.[3]

Support in common programming and scripting languages

[edit]

As you look at the table below, keep in mind that bitwise operators often do not behave exactly like logical operators, even if both arguments are of 0, 1 or Boolean type.

Examples:

  • In JavaScript, each of the following 3 expressions evaluates to false:
    (true & true) === (true && true),
    (false | false) === (false || false),
    (1 & 2) === (1 && 2).
  • In PHP, each of the following 3 expressions evaluates to false:
    (true & true) === (true && true),
    (0 | 0) === (0 || 0),
    (1 & 2) === (1 && 2).
Boolean operators in various languages
Language Eager operators Short-circuit operators Result type
Advanced Business Application Programming (ABAP) none and, or Boolean[a]
Ada and, or and then, or else Boolean
ALGOL 68 and, &, ∧ ; or, ∨ andf , orf (both user defined) Boolean
APL , , (nand), (nor), etc. :AndIf, :OrIf Boolean[a]
awk none &&, || Boolean
Bash none &&, || Boolean
C, Objective-C &, |[b] &&, ||, ?[5] int (&, |, &&,||), opnd-dependent (?)
C++[c] none &&, ||, ?[6] Boolean (&&,||), opnd-dependent (?)
C# &, | &&, ||, ?, ?? Boolean (&&,||), opnd-dependent (?, ??)
ColdFusion Markup Language (CFML) none AND, OR, &&, || Boolean
D[d] &, | &&, ||, ? Boolean (&&,||), opnd-dependent (?)
Eiffel and, or and then, or else Boolean
Erlang and, or andalso, orelse Boolean
Fortran[e] .and., .or. .and., .or. Boolean
Go, Haskell, OCaml none &&, || Boolean
Java, MATLAB, R, Swift &, | &&, || Boolean
JavaScript none &&, &&=, ||, ||= Last value
Julia none &&, || Last value
Lasso none and, or, &&, || Last value
Kotlin and, or &&, || Boolean
Lisp, Lua, Scheme none and, or Last value
MUMPS (M) &, ! none Numeric
Modula-2 none AND, OR Boolean
Oberon none &, OR Boolean
OCaml land, lor[7] &&, || Boolean
Pascal and, or[f][g] and_then, or_else[g] Boolean
Perl &, | &&, and, ||, or Last value
PHP none &&, and, ||, or Boolean
POSIX shell (command list) none &&, || Last value (exit)
PowerShell Scripting Language none -and, -or Boolean
Python &, | and, or Last value
Ruby &, | &&, and, ||, or[8] Last value
Rust &, | &&, ||[9] Boolean
Smalltalk &, | and:, or:[h] Boolean
Standard ML Un­known andalso, orelse Boolean
TTCN-3 none and, or[10] Boolean
Beckhoff TwinCAT® (IEC 61131-3)[i] AND, OR AND_THEN,[11] OR_ELSE[12] Boolean
Visual Basic .NET And, Or AndAlso, OrElse Boolean
Visual Basic, Visual Basic for Applications (VBA) And, Or Select Case[j] Numeric
Wolfram Language And @@ {...}, Or @@ {...} And, Or, &&, || Boolean
ZTT &, | none Boolean
  1. ^ a b ABAP and APL have no distinct boolean type.
  2. ^ The bitwise operators behave like boolean operators when both arguments are of type bool or take only the values 0 or 1.[4]
  3. ^ When overloaded, the operators && and || are eager and can return any type.
  4. ^ This only applies to runtime-evaluated expressions, static if and static assert. Expressions in static initializers or manifest constants use eager evaluation.
  5. ^ Fortran operators are neither short-circuit nor eager: the language specification allows the compiler to select the method for optimization.
  6. ^ ISO/IEC 10206:1990 Extended Pascal allows, but does not require, short-circuiting.
  7. ^ a b Delphi and Free Pascal default to short circuit evaluation. This may be changed by compiler options but does not seem to be used widely.
  8. ^ Smalltalk uses short-circuit semantics as long as the argument to and: is a block (e.g., false and: [Transcript show: 'Wont see me']).
  9. ^ The norm IEC 61131-3 doesn't actually define if AND and OR use short-circuit evaluation and it doesn't define the operators AND_THEN and OR_ELSE. The entries in the table show how it works for Beckhoff TwinCAT®.
  10. ^ BASIC languages that supported CASE statements did so by using the conditional evaluation system, rather than as jump tables limited to fixed labels.

Common use

[edit]

Avoiding undesired side effects of the second argument

[edit]

Usual example, using a C-based language:

int denom = 0;
if (denom != 0 && num / denom)
{
    ... // ensures that calculating num/denom never results in divide-by-zero error   
}

Consider the following example:

int a = 0;
if (a != 0 && myfunc(b))
{
    do_something();
}

In this example, short-circuit evaluation guarantees that myfunc(b) is never called. This is because a != 0 evaluates to false. This feature permits two useful programming constructs.

  1. If the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument.
  2. It permits a construct where the first expression guarantees a condition without which the second expression may cause a run-time error.

Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches:

bool is_first_char_valid_alpha_unsafe(const char *p)
{
    return isalpha(p[0]); // SEGFAULT highly possible with p == NULL
}

bool is_first_char_valid_alpha(const char *p)
{
    return p != NULL && isalpha(p[0]); // 1) no unneeded isalpha() execution with p == NULL, 2) no SEGFAULT risk
}

Idiomatic conditional construct

[edit]

Since minimal evaluation is part of an operator's semantic definition and not an optional optimization, a number of coding idioms rely on it as a succinct conditional construct. Examples include:

Perl idioms:

some_condition or die;    # Abort execution if some_condition is false
some_condition and die;   # Abort execution if some_condition is true

POSIX shell idioms:[13]

modprobe -q some_module && echo "some_module installed" || echo "some_module not installed"

This idiom presumes that echo cannot fail.

Possible problems

[edit]

Untested second condition leads to unperformed side effect

[edit]

Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code

if (expressionA && myfunc(b)) {
    do_something();
}

if myfunc(b) is supposed to perform some required operation regardless of whether do_something() is executed, such as allocating system resources, and expressionA evaluates as false, then myfunc(b) will not execute, which could cause problems. Some programming languages, such as Java, have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.

Problems with unperformed side effect statements can be easily solved with proper programming style, i.e., not using side effects in boolean statements, as using values with side effects in evaluations tends to generally make the code opaque and error-prone.[14]

Reduced efficiency due to constraining optimizations

[edit]

Short-circuiting can lead to errors in branch prediction on modern central processing units (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in ray tracing.[clarification needed] Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.[citation needed]

An example of a compiler unable to optimize for such a case is Java's Hotspot virtual machine (VM) as of 2012.[15]

See also

[edit]

References

[edit]
  1. ^ Edsger W. Dijkstra "On a somewhat disappointing correspondence", EWD1009-0, 25 May 1987 full text
  2. ^ "Shell Command Language". pubs.opengroup.org.
  3. ^ Bergstra, Jan A.; Ponse, A.; Staudt, D.J.C. (2010). "Short-circuit logic". arXiv:1010.3674 [cs.LO].
  4. ^ ISO/IEC 9899 standard, sections 6.2.5, 6.3.1.2, 6.5 and 7.16.
  5. ^ ISO/IEC 9899 standard, section 6.5.13
  6. ^ ISO/IEC IS 14882 draft.
  7. ^ "OCaml - the OCaml language".
  8. ^ "operators - Documentation for Ruby 3.3". docs.ruby-lang.org. Retrieved 2024-04-02.
  9. ^ "std::ops - Rust". doc.rust-lang.org. Retrieved 2019-02-12.
  10. ^ ETSI ES 201 873-1 V4.10.1, section 7.1.4
  11. ^ "Beckhoff Information System - English". infosys.beckhoff.com. Retrieved 2021-08-16.
  12. ^ "Beckhoff Information System - English". infosys.beckhoff.com. Retrieved 2021-08-16.
  13. ^ "What does || mean in bash?". stackexchange.com. Retrieved 2019-01-09.
  14. ^ "Referential Transparency, Definiteness and Unfoldability" (PDF). Itu.dk. Retrieved 2013-08-24.
  15. ^ Wasserman, Louis (11 July 2012). "Java: What are the cases in which it is better to use unconditional AND (& instead of &&)". Stack Overflow.