Recent

Author Topic: How is "not condition1 and condition2" evaluated?  (Read 9365 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How is "not condition1 and condition2" evaluated?
« Reply #45 on: July 15, 2019, 12:43:15 pm »
Thaddy: what do you get with the following?
Code: Pascal  [Select][+][-]
  1.   WriteLn('[unvarnished outcome]          True or False and False ',True or  False and False);
  2.   WriteLn('[''and'' has precedence]       True or (False and False) ',True or  (False and False));
  3.   WriteLn('[left-to-right has precedence] (True or False) and False ',(True or  False) and False);

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How is "not condition1 and condition2" evaluated?
« Reply #46 on: July 15, 2019, 01:51:08 pm »
In fact, it seems logical, otherwise it was difficult to predict the behavior of such an expression:
Code: Pascal  [Select][+][-]
  1. if Assigned(P) and (P^...)...
But for some reason in the documentation this was not explicitly mentioned.
It is really unclear in current docs, so I recently reported exactly this, then Michael updated the docs for 3.2 -- https://bugs.freepascal.org/view.php?id=35661

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: How is "not condition1 and condition2" evaluated?
« Reply #47 on: July 15, 2019, 03:28:04 pm »
It is really unclear in current docs, so I recently reported exactly this, then Michael updated the docs for 3.2 -- https://bugs.freepascal.org/view.php?id=35661
Thanks. In the repository "fpcdocs" in the file "ref.tex" there is an addition:
Code: LaTeX  [Select][+][-]
  1. A notable exception to this behaviour is the evaluation of boolean expressions:
  2. if short-circuit boolean evaluation is enabled (the default) then the
  3. compiler will evaluate from left to right, but will still respect
  4. precedence, i.e. in parts with equal precedence the left operand will always
  5. be evaluated before the right one. Thus, the following example:
  6. \begin{verbatim}
  7.  True or True and False
  8. \end{verbatim}
  9. will evaluate to \var{True}, because it is equivalent to
  10. \begin{verbatim}
  11.  True or (True and False)
  12. \end{verbatim}
  13. \end{remark}

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: How is "not condition1 and condition2" evaluated?
« Reply #48 on: July 15, 2019, 06:12:44 pm »
Following functions are also useful to experiment:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. function t(i:integer):boolean;
  3.         begin
  4.         result:= true;
  5.         writeln('t(',i,') called');
  6.         end;
  7. function f(i:integer):boolean;
  8.         begin
  9.         result:= false;
  10.         writeln('f(',i,') called');
  11.         end;
  12. begin
  13.         writeln('not f(1) and t(2) or f(3)');
  14.         writeln(not f(1) and t(2) or f(3));
  15. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: How is "not condition1 and condition2" evaluated?
« Reply #49 on: July 15, 2019, 09:18:08 pm »
Quote
He is probably spinning by now.
Permanently.... :)
Specialize a type, not a var.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: How is "not condition1 and condition2" evaluated?
« Reply #50 on: July 15, 2019, 10:11:17 pm »
I've been reading this thread and wondering when someone was going to show that the "left to right" evaluation only applies to operators that have the same precedence and precedence varies among the Boolean operators.
...

I remember that, and remember finding it hard to remember (precedence of "and" vs "or"). :) So I use parentheses even when not needed.
« Last Edit: July 15, 2019, 10:15:40 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How is "not condition1 and condition2" evaluated?
« Reply #51 on: July 16, 2019, 04:00:43 am »
I always use parenthesis in these cases.  It forces a precedence and is clear to read.  (Even if I don't "really" need them)

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: How is "not condition1 and condition2" evaluated?
« Reply #52 on: July 16, 2019, 05:19:42 am »
From different tests, following seem to be the 'rules':

1. Evaluation is from left to right.
2. 'not' binds only to the next term, not beyond it. Hence 'not a or/and b' means '(not a) or/and b'. It does not mean 'not(a or/and b)'
3. Similarly, 'and' only binds to next term and not beyond, hence 'a and b or c' means '(a and b) or c' and not 'a and (b or c)'
4. Statement beyond 'or' is evaluated only if current value is false. If current value is true, evaluation of statement ends on encountering 'or'.
5. Term beyond 'and' is evaluated only if current value is true. If current value is false, control jumps beyond the term after 'and', putting current value there as false. (Evaluation of further terms will only occur if an 'or' is encountered).


Above is helping me for actual coding.
« Last Edit: July 16, 2019, 05:25:31 pm by rnfpc »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How is "not condition1 and condition2" evaluated?
« Reply #53 on: July 16, 2019, 05:54:40 am »
I always use parenthesis in these cases.  It forces a precedence and is clear to read.  (Even if I don't "really" need them)

Yeah, I do that too except for very simple expressions. But know what? I've just realized that because of that I have never interiorized the precedence rules. I know them grosso-modo but I don't really grok them.

Which means one (more) thing for me to stop and study carefully :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: How is "not condition1 and condition2" evaluated?
« Reply #54 on: July 16, 2019, 06:12:18 am »
Which means one (more) thing for me to stop and study carefully :)
I think the simplest way of understanding the Boolean precedence rules is how Jensen & Wirth presented them in their Pascal report.

Basically (precedence-wise),

not is equivalent to the unary minus
and is equivalent to the multiplication operator (*)
or is equivalent to the addition operator (+)

The really significant difference between an arithmetic expression and a Boolean expression is that, in the case of an arithmetic expression it is normally necessary to fully evaluate it to determine the result.  In the case of a Boolean expression, there are many cases/constructions where it isn't necessary to evaluate the entire expression to determine the result.  IOW, in the case of Boolean expressions, partial evaluation is possible to determine the result, which is usually not possible in the case of arithmetic expressions.

As you and others stated, when in doubt, and/or for readability purposes, parentheses should be used.  It's usually a good idea to use them when the expression contains a mix of and(s) and or(s).  It makes the programmer's intention clear and the code logically cleaner.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How is "not condition1 and condition2" evaluated?
« Reply #55 on: July 16, 2019, 06:24:57 am »
@440bx: That post just won a honor place in my "Important notes" file.  :D
Thanks.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: How is "not condition1 and condition2" evaluated?
« Reply #56 on: July 16, 2019, 06:34:05 am »
@440bx: That post just won a honor place in my "Important notes" file.  :D
Thanks.
My pleasure.  I hope other people find it helpful too.  The equivalences comes directly from Jensen and Wirth's presentation in their Pascal report.  IOW, we both owe them that clear, understandable explanation.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How is "not condition1 and condition2" evaluated?
« Reply #57 on: July 16, 2019, 09:03:51 am »
That's not correct Howard. Hence the bug report against documentation. Pure Boolean operators are equal weight.
(Boole would probably have turned in his grave)
No, Howard is correct. See compiler/tokens.pas:
Code: Pascal  [Select][+][-]
  1.   { sub_expr(opmultiply) is need to get -1 ** 4 to be
  2.     read as - (1**4) and not (-1)**4 PM }
  3.   toperator_precedence=(
  4.     opcompare,
  5.     opaddition,
  6.     opmultiply,
  7.     oppower
  8.   );
  9.  
  10. { snip }
  11.  
  12. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  13.       ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  14.        [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  15.        [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  16.         _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  17.        [_STARSTAR] );
  18.  
As you can see _OP_OR is at a different level than _OP_AND.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: How is "not condition1 and condition2" evaluated?
« Reply #58 on: July 16, 2019, 04:46:01 pm »
4. Term beyond 'and' is always evaluated to find value of full expression, i.e. '... and ...'

I expect the term beyond 'and' only to be evaluated when the first expression is true.

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: How is "not condition1 and condition2" evaluated?
« Reply #59 on: July 16, 2019, 05:05:47 pm »
4. Term beyond 'and' is always evaluated to find value of full expression, i.e. '... and ...'

I expect the term beyond 'and' only to be evaluated when the first expression is true.
Yes, please see my modified list of rules.
« Last Edit: July 16, 2019, 05:17:21 pm by rnfpc »

 

TinyPortal © 2005-2018