Recent

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

rnfpc

  • Jr. Member
  • **
  • Posts: 91
How is "not condition1 and condition2" evaluated?
« on: July 12, 2019, 09:09:53 am »
In the setting of "if", how is "not condition1 and condition2" evaluated?

It is evaluated as:

Code: Pascal  [Select]
  1. (not condition1) and condition2

or:

Code: Pascal  [Select]
  1. not (condition1 and condition2)

because: "(not false) and false" is false
but: "not (false and false)" is true.

Thanks for your insight.
« Last Edit: July 12, 2019, 09:12:02 am by rnfpc »

PascalDragon

  • Hero Member
  • *****
  • Posts: 726
  • Compiler Developer
Re: How is "not condition1 and condition2" evaluated?
« Reply #1 on: July 12, 2019, 09:15:54 am »
not has a stronger binding than and. Thus it's evaluated as (not condition1) and condition2.

WooBean

  • Jr. Member
  • **
  • Posts: 89
Re: How is "not condition1 and condition2" evaluated?
« Reply #2 on: July 12, 2019, 09:26:29 am »
See Table 12.1: Precedence of operators at  https://www.freepascal.org/docs-html/ref/refch12.html#x139-16100012
Win7/64, Lazarus 1.8 win64-win64, FPC 3.0.4

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: How is "not condition1 and condition2" evaluated?
« Reply #3 on: July 12, 2019, 09:27:22 am »
Actually all Boolean operators have equal weight and evaluation in Pascal is left to right, so not binds only to the first condition and and  binds the result to the second condition.
Sven is right, but maybe not clear enough.
Your own actual examples are exact and correct for that reason.
Some programmers therefor often use a style as suggested by Sven: even if the ( ) are not needed, like your first result, you may still want to use them for quicker readability.  The second example needs the brackets.
I am not one of those, but it is a valid solution and does not impact code generation.
[edit]
This also means that the documentation link above is not clear regarding precedence of Boolean operators. I will file a bug against it.

Reported as 0035834
« Last Edit: July 12, 2019, 09:45:36 am by Thaddy »
also related to equus asinus.

PascalDragon

  • Hero Member
  • *****
  • Posts: 726
  • Compiler Developer
Re: How is "not condition1 and condition2" evaluated?
« Reply #4 on: July 12, 2019, 09:55:11 am »
See Table 12.1: Precedence of operators at  https://www.freepascal.org/docs-html/ref/refch12.html#x139-16100012
Ah, that's where the table I was looking for is.  :-[

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: How is "not condition1 and condition2" evaluated?
« Reply #5 on: July 12, 2019, 10:07:22 am »
Ah, that's where the table I was looking for is.  :-[
Yes, I hope you agree precedence for Boolean operators is unclear.
also related to equus asinus.

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: How is "not condition1 and condition2" evaluated?
« Reply #6 on: July 12, 2019, 12:22:38 pm »
[…] evaluation in Pascal is left to right […]
FPC may re-arrange expressions though as it think it’s best. One heuristic is “evaluate more ‘complex’ sub-expressions first, and then turn to ‘simple’ sub-expressions.”

With the advent of and_then and or_else (extended Pascal) we can say “left to right” for those two.

I still don’t understand, how the precedence table is equivocal. What is this “weight” you’re talking about?
Yours Sincerely
Kai Burghardt

ccrause

  • Full Member
  • ***
  • Posts: 195
Re: How is "not condition1 and condition2" evaluated?
« Reply #7 on: July 12, 2019, 12:33:17 pm »
Actually all Boolean operators have equal weight and evaluation in Pascal is left to right, ...

Reference?  The FPC documentation agrees with the behaviour of the latest stable compiler:
Code: Pascal  [Select]
  1.   b1 := true;
  2.   b2 := true;
  3.   b3 := false;
  4.  
  5.   if b1 or b2 = b3 then
  6.     WriteLn('Unexpected')
  7.   else
  8.     WriteLn('Expected');
  9.  
  10.   if b3 = b2 or b1 then
  11.     WriteLn('Unexpected - left to right evaluation')
  12.   else
  13.     WriteLn('Expected based on precedence rules');


Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: How is "not condition1 and condition2" evaluated?
« Reply #8 on: July 12, 2019, 12:56:03 pm »
[…]
= belongs to the category of “relational operators”, which comes third in precedence. I still don’t understand how and where any ambiguity may arise.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: How is "not condition1 and condition2" evaluated?
« Reply #9 on: July 12, 2019, 03:56:34 pm »
What is this “weight” you’re talking about?
Quite simply, mathematical operators have a defined precedence. See here: https://en.wikipedia.org/wiki/Order_of_operations
These operators differ in weight.
Boolean operators do not have a defined precedence, they are all equal and their evaluation by the compiler is left to right.

Clear? (This is actually basics)

Whereas mathematical operators must all be evaluated because of weight - or order, if that is more clear to you - , Boolean operators can be shortcut once an impossibility is determined.

(To me it is really incomprehensible that some programmers do not understand Boolean logic)
« Last Edit: July 12, 2019, 04:07:03 pm by Thaddy »
also related to equus asinus.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: How is "not condition1 and condition2" evaluated?
« Reply #10 on: July 12, 2019, 04:31:37 pm »
Actually all Boolean operators have equal weight and evaluation in Pascal is left to right, so not binds only to the first condition and and  binds the result to the second condition.
Well, unary operators have to have higher precedence.

In an expression
  a and not b
you have to evaluate "not b" before evaluation of "and".

lucamar

  • Hero Member
  • *****
  • Posts: 2145
Re: How is "not condition1 and condition2" evaluated?
« Reply #11 on: July 12, 2019, 06:24:01 pm »
In an expression
  a and not b
you have to evaluate "not b" before evaluation of "and".

That is because to do otherwise makes no sense. You can't do "(a and not) b" just as you can't do "(a +-) b". There's no other option than "a and (not b)" and "a + (-b)".
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: How is "not condition1 and condition2" evaluated?
« Reply #12 on: July 12, 2019, 06:37:10 pm »
Yes, of course. But nevertheless, it means that "not" has precedence over other boolean operators, which contradicts earlier statement that they all are equal.

lucamar

  • Hero Member
  • *****
  • Posts: 2145
Re: How is "not condition1 and condition2" evaluated?
« Reply #13 on: July 12, 2019, 07:38:35 pm »
Yes, of course. But nevertheless, it means that "not" has precedence over other boolean operators, which contradicts earlier statement that they all are equal.

Not really. The boolean expression is parsed left to right. Iin this case, the parser sees the "and" so it expects:
Code: [Select]
boolean_expression and boolean_expressionso it examines things closely and finds "a" on the left and "not" on the right. That "not" cannot be by itself, so it looks a little more and decides that the right expression is "not b". Done.

The key here is that the "not" can't be interpreted alone: it introduces a new boolean expression. The net result is to make it look as if it had precedence, but it isn't really so.

Ah .. that is, of course, a very simplified explanation, not necessarily what the actual parser really does.
« Last Edit: July 12, 2019, 07:41:14 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: How is "not condition1 and condition2" evaluated?
« Reply #14 on: July 12, 2019, 07:52:45 pm »
Yes, of course. But nevertheless, it means that "not" has precedence over other boolean operators, which contradicts earlier statement that they all are equal.
No. You really do not understand this. Not has equal weight. a and not b will negate b. Basics. Real basics.
also related to equus asinus.