{"id":5775,"date":"2023-11-01T18:38:15","date_gmt":"2023-11-02T02:38:15","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=5775"},"modified":"2023-11-01T18:38:15","modified_gmt":"2023-11-02T02:38:15","slug":"exploiting-short-circuit-evaluation-for-conditional-execution","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2023\/11\/01\/exploiting-short-circuit-evaluation-for-conditional-execution\/","title":{"rendered":"Exploiting Short-Circuit Evaluation for conditional execution"},"content":{"rendered":"\n<p>TLDR:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>T &amp;&amp; X is equivalent to \"if( T) then run X\"\nT || X is equivalent to \"if(!T) then run X\"\r<\/code><\/pre>\n\n\n\n<p>I don&#8217;t exploit this too much in C\/C++ because it&#8217;s hard to read (and therefore hard to keep track of it to make sure it&#8217;s bug free) and most often I&#8217;m interested in the output value so I have to watch out for the side effects. However this is common in Bash scripts<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Domination Property<\/h2>\n\n\n\n<p>In languages that expressions evaluates to a value, sometimes if-statements can be replaced by short-circuit evaluation because short-circuit evaluation exploits the <strong><em>domination<\/em><\/strong> property of AND and OR logic operations:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>\\newcommand{\\Hquad}{\\hspace{0.5em}}\n\\begin{alignat*}{2}\n0 \\Hquad &amp; \\mathrm{AND} &amp;  \\Hquad X  =  0 \\\\\n1 \\Hquad &amp; \\mathrm{OR} &amp; \\Hquad X =  1\n\\end{alignat*}<\/pre><\/div>\n\n\n\n<p>When you FIRST run into the dominant value for the binary operation (0 for AND) and (1 for OR), evaluate no further (i.e. skip the rest) because rest won&#8217;t change the overall result away from the dominant value.<\/p>\n\n\n\n<p>So in this use case (emulating if-then statements), what the latter expression <em>X<\/em> evaluates to or what the combined logic value is <strong>irrelevant<\/strong>. We are merely tricking the short-circuit mechanism to trip (short) to NOT evaluate based on what the earlier expression turned out. Action is the &#8216;norm&#8217;. Conditional inaction is the essence of this idiom.<\/p>\n\n\n\n<p>The dual of domination property is idempotent, which is easier to reason because if pre-condition (say T) forces overall expression to boil down to the expression we want to conditionally execute (say X), we are stuck evaluating X if condition T is met. <\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>\\newcommand{\\Hquad}{\\hspace{0.5em}}\n\\begin{alignat*}{2}\n1 \\Hquad &amp; \\mathrm{AND} &amp;  \\Hquad X  =  X \\\\\n0 \\Hquad &amp; \\mathrm{OR} &amp; \\Hquad X =  X\n\\end{alignat*}<\/pre><\/div>\n\n\n\n<p>These 2 possibilities (domination and idempotency) partitions to space (choices) of possibilities (i.e. cover all possible combinations), in other words there are no other scenarios than described. So the precondition T decides whether you run X or not, which is the equivalent of an if-then statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Operator (function) view of logic domination [Functional programming perspective]<\/h2>\n\n\n\n<p>By grouping the first value and the binary logic operator with a pair of parenthesis, in dominance view<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>(0 AND) is also called the &#8216;clear&#8217; operator<\/li>\n\n\n\n<li>(1 OR) is also called the &#8216;set&#8217; operator<\/li>\n<\/ul>\n\n\n\n<p>but this view is not too interesting for our case because we are not interested in what the conditional expression and the overall expression evaluates to, which is signified by &#8216;clear&#8217; and &#8216;set&#8217;.<\/p>\n\n\n\n<p>On the other hand, (1 AND) and (0 OR) are pass-through (idempotent) operators which passes the evaluation to the latter expression X.<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>\\newcommand{\\Hquad}{\\hspace{0.5em}}\n\\begin{alignat*}{2}\n(1 \\Hquad &amp; \\mathrm{AND}) \\Hquad &amp; \\circ &amp;  \\Hquad X  =  X \\\\\n(0 \\Hquad &amp; \\mathrm{OR}) \\Hquad &amp; \\circ &amp; \\Hquad X =  X\n\\end{alignat*}<\/pre><\/div>\n\n\n\n<p>This reads <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>(1 AND): pass-through (evaluate latter expression) if (earlier expression is) TRUE<\/li>\n\n\n\n<li>(0 OR): pass-through (evaluate latter expression) if (earlier expression is) FALSE<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s call T the condition to test (the &#8216;if&#8217;-condition). The expression to run remain X. <\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>\\newcommand{\\Hquad}{\\hspace{0.5em}}\n\\begin{alignat}{2}\n(T \\Hquad \\mathrm{AND}) X &amp; = \\overline{f_T}(X) \\\\\n(T \\Hquad \\mathrm{OR}) X &amp; = f_T(X)\n\\end{alignat}<\/pre><\/div>\n\n\n\n<p>where <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"katex-eq\" data-katex-display=\"false\">\\overline{f_T}<\/span> reads &#8220;Run if T is false&#8221; <\/li>\n\n\n\n<li><span class=\"katex-eq\" data-katex-display=\"false\">f_T<\/span> reads &#8220;Run if T is true&#8221;.<\/li>\n<\/ul>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_5775\" class=\"pvc_stats all  \" data-element-id=\"5775\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>TLDR: I don&#8217;t exploit this too much in C\/C++ because it&#8217;s hard to read (and therefore hard to keep track of it to make sure it&#8217;s bug free) and most often I&#8217;m interested in the output value so I have &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2023\/11\/01\/exploiting-short-circuit-evaluation-for-conditional-execution\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_5775\" class=\"pvc_stats all  \" data-element-id=\"5775\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[30,25,39,19],"tags":[],"class_list":["post-5775","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-cpp","category-linux","category-low-level"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=5775"}],"version-history":[{"count":7,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5775\/revisions"}],"predecessor-version":[{"id":5782,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5775\/revisions\/5782"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=5775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=5775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=5775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}