BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

something I came to think about ... (logical operators in if
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Nicolai Hansen
Guest





PostPosted: Wed Nov 12, 2003 1:01 pm    Post subject: something I came to think about ... (logical operators in if Reply with quote



NB! This is not something I have tried - just a thought that struck me
after reading some c++ NG (you sometimes get weird thoughts then) :)

Some logic operator law states that (a or b) is the same as (not (a
and b)). However, I now have doubt that this holds true for if
statements ;)

Consider the following two statements:

1) if (p=nil) or (p->d=nil) then

2) if not ((p<>nil) and (p->d<>nil)) then

(consider p as a pointer to an object with one element, d, being
another pointer. The above is then a general check for NULL data)

1) If either p=nil, or the element d=nil, this returns true. But
because of the compiler's interpretation of OR, we know not if
(p=nil)? or (p->d=nil)? is executed first. If (p->d=nil) is executed
first when (p=nil) is actually true, this will result in an access
violation.

2) With AND it is guaranteed that (p<>nil) is executed first.
(p->d<>nil) is only ever checked IF p<>nil holds true. This leads to a
different behaviour than case 1).

And then I got scared :p Cos I seem to recall reading somewhere that
the compiler do adjust these IF statements, exchanging "not (a AND b)"
with "(a OR b)" automatically. But it is not the same?

Is this true? (haha, got that one? "true" :p) -or- (again! haha) am I
just starting another useless theoretical thread about nothing?
Back to top
AlanGLLoyd
Guest





PostPosted: Wed Nov 12, 2003 2:17 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote



In article <d96764ff.0311120501.48c2abfa (AT) posting (DOT) google.com>, [email]nic (AT) aub (DOT) dk[/email]
(Nicolai Hansen) writes:

Quote:
1) If either p=nil, or the element d=nil, this returns true. But
because of the compiler's interpretation of OR, we know not if
(p=nil)? or (p->d=nil)? is executed first. If (p->d=nil) is executed
first when (p=nil) is actually true, this will result in an access
violation.

Not so (at least not in D3), D3 Help says ...

"Object Pascal supports two different models of code generation for the and and
or operators: complete evaluation and short-circuit (partial) evaluation.
Complete evaluation means that every operand of a Boolean expression built from
the and and or operators is guaranteed to be evaluated, even when the result of
the entire expression is already known.
<snip>
Short-circuit evaluation guarantees strict left-to-right evaluation and that
evaluation stops as soon as the result of the entire expression becomes
evident.
<snip>
Short-circuit evaluation also makes possible the evaluation of constructs that
would not otherwise be legal."

Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]

Back to top
Dave Langers
Guest





PostPosted: Wed Nov 12, 2003 2:18 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote



Quote:
Some logic operator law states that (a or b) is the same as (not (a
and b)).

Of course you meant (a or b) equals (not ((not a) and (not b))) !

Quote:
However, I now have doubt that this holds true for if
statements ;)

Consider the following two statements:
1) if (p=nil) or (p->d=nil) then
2) if not ((p<>nil) and (p->d<>nil)) then
(consider p as a pointer to an object with one element, d, being
another pointer. The above is then a general check for NULL data)

I don't know the "p->d" notation; is it Delphi, and if so, is it the
same as something like "p^.d"?

Quote:
1) If either p=nil, or the element d=nil, this returns true. But
because of the compiler's interpretation of OR, we know not if
(p=nil)? or (p->d=nil)? is executed first. If (p->d=nil) is executed
first when (p=nil) is actually true, this will result in an access
violation.
2) With AND it is guaranteed that (p<>nil) is executed first.
(p->d<>nil) is only ever checked IF p<>nil holds true. This leads to a
different behaviour than case 1).

I think I recognize this from a C-type language where there is a
difference between the operators & and | versus && and ||, in Delphi it
is controlled by the {$B} compiler directive.

However, if a compiler is intelligent enough to know that he doesn't
have to evaluate (p^.d <> nil) after (p <> nil) has already evaluated to
FALSE in an and-statement, why wouldn't he be intelligent enough to know
that he doesn't have to evaluate (p^.d = nil) after (p = nil) has
already evaluated to TRUE in an or-statement?
(I haven't tried this in a piece of code, and the help section on
complete versus short-circuit Boolean evaluation only shows a few
'and'-examples, but it would be kind of silly if 'or' would behave
differently, wouldn't it?)

Quote:
And then I got scared :p Cos I seem to recall reading somewhere that
the compiler do adjust these IF statements, exchanging "not (a AND b)"
with "(a OR b)" automatically. But it is not the same?
Is this true? (haha, got that one? "true" :p) -or- (again! haha) am I
just starting another useless theoretical thread about nothing?

Yeah, haha ;-S
--
M.vr.gr.
Dave
("d-dot-langers-at-wxs-dot-nl")


Back to top
Nicolai Hansen
Guest





PostPosted: Wed Nov 12, 2003 3:07 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote


"Dave Langers" <spam.messages (AT) annoy (DOT) us> skrev i en meddelelse
news:botffj$1hpsg4$1 (AT) ID-86745 (DOT) news.uni-berlin.de...
Quote:
Some logic operator law states that (a or b) is the same as (not (a
and b)).

Of course you meant (a or b) equals (not ((not a) and (not b))) !

I just might :)

Quote:
I don't know the "p->d" notation; is it Delphi, and if so, is it the
same as something like "p^.d"?

Oops - as I started, I got to think about this while reading a post in a c++
NG Wink
replace "p->d" with "p.d" :)

Quote:
However, if a compiler is intelligent enough to know that he doesn't
have to evaluate (p^.d <> nil) after (p <> nil) has already evaluated to
FALSE in an and-statement, why wouldn't he be intelligent enough to know
that he doesn't have to evaluate (p^.d = nil) after (p = nil) has
already evaluated to TRUE in an or-statement?

I was frightened it might evaluate it from right to left if it felt like it
Wink
But from reading Alan's post, I think I worry without a cause :D

Anyway, it was just one of these silly small things I was thinking about
while
I was supposed to work ;)




Back to top
Nicolai Hansen
Guest





PostPosted: Wed Nov 12, 2003 3:07 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

Quote:
Short-circuit evaluation guarantees strict left-to-right evaluation and
that
evaluation stops as soon as the result of the entire expression becomes
evident.

All I can say is 'phew', my world lasts another day :p




Back to top
René Allan Larsen
Guest





PostPosted: Wed Nov 12, 2003 6:41 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

In article <d96764ff.0311120501.48c2abfa (AT) posting (DOT) google.com>, Nicolai Hansen wrote:
Quote:

NB! This is not something I have tried - just a thought that struck me
after reading some c++ NG (you sometimes get weird thoughts then) :)

Some logic operator law states that (a or b) is the same as (not (a
and b)). However, I now have doubt that this holds true for if
statements ;)

Consider the following two statements:

1) if (p=nil) or (p->d=nil) then

2) if not ((p<>nil) and (p->d<>nil)) then

(consider p as a pointer to an object with one element, d, being
another pointer. The above is then a general check for NULL data)

1) If either p=nil, or the element d=nil, this returns true. But
because of the compiler's interpretation of OR, we know not if
(p=nil)? or (p->d=nil)? is executed first. If (p->d=nil) is executed
first when (p=nil) is actually true, this will result in an access
violation.

That is why you should always use "Boolean short-circuit evaluation" (which Delphi
defaults to anyway):

The Delphi compiler supports two modes of evaluation for the and and or
operators: complete evaluation and short-circuit (partial) evaluation. Complete
evaluation means that each conjunct or disjunct is evaluated, even when the
result of the entire expression is already determined. Short-circuit evaluation
means strict left-to-right evaluation that stops as soon as the result of the
entire expression is determined. For example, if the expression A and B is
evaluated under short-circuit mode when A is False, the compiler won’t evaluate
B; it knows that the entire expression is False as soon as it evaluates A.

Short-circuit evaluation is usually preferable because it guarantees minimum
execution time and, in most cases, minimum code size. Complete evaluation is
sometimes convenient when one operand is a function with side effects that
alter the execution of the program.

Short-circuit evaluation also allows the use of constructions that might
otherwise result in illegal runtime operations.

Quote:
2) With AND it is guaranteed that (p<>nil) is executed first.
(p->d<>nil) is only ever checked IF p<>nil holds true. This leads to a
different behaviour than case 1).

No. If you switch to "complete evaluation", (p->d<>nil) is checked EVEN if p<>nil
holds true.

Regards, René


Back to top
AlanGLLoyd
Guest





PostPosted: Wed Nov 12, 2003 7:09 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

In article <3fb24cfe$0$27402$edfadb0f (AT) dread16 (DOT) news.tele.dk>, "Nicolai Hansen"
<nic (AT) aub (DOT) dk> writes:

Quote:
But from reading Alan's post, I think I worry without a cause :D


It's always useful to read and understand as much as one cane before asking
questions, then the questions become really helpful and can confirm (or deny)
the mental concept one has built from the printed words.

Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]

Back to top
VBDis
Guest





PostPosted: Sat Nov 15, 2003 6:57 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

Im Artikel <d96764ff.0311120501.48c2abfa (AT) posting (DOT) google.com>, [email]nic (AT) aub (DOT) dk[/email]
(Nicolai Hansen) schreibt:

Quote:
1) if (p=nil) or (p->d=nil) then

Apart from the "complete boolean eval" topic there's another trick in this
condition. Exchange both comparisons, and you'll get an AV if p=nil. Conditions
of the above form often are used to check an pointer for non-nil, before
accessing members of the data structure that is referenced by the pointer. Of
course such cascaded tests only work properly with short-circuit evaluation. If
you want to make such code bullet proof, you must split the condition into
nested IF statements, which are executed in a predictable manner.

Perhaps you now see the difference between boolean logic in theory, and the way
how conditions are used and evaluated in programming practice. Pure and simple
math often is not sufficient to write working code, as soon as numerical
constraints, dependencies or side effects come into play.

DoDi

Back to top
Martin Harvey (Demon Acco
Guest





PostPosted: Sat Nov 15, 2003 7:42 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

On 12 Nov 2003 05:01:18 -0800, [email]nic (AT) aub (DOT) dk[/email] (Nicolai Hansen) wrote:

Quote:
Some logic operator law states that (a or b) is the same as (not (a
and b)).

De Morgan's law.

Quote:
However, I now have doubt that this holds true for if
statements Wink

Short circuit evaluation may change the legality of some constructs.

MH.


Back to top
David Reeve
Guest





PostPosted: Sun Nov 16, 2003 12:52 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote


"VBDis" <vbdis (AT) aol (DOT) com> wrote

Quote:
Im Artikel <d96764ff.0311120501.48c2abfa (AT) posting (DOT) google.com>, [email]nic (AT) aub (DOT) dk[/email]

[snip]
Quote:

Perhaps you now see the difference between boolean logic in theory, and
the way
how conditions are used and evaluated in programming practice. Pure and
simple
math often is not sufficient to write working code, as soon as numerical
constraints, dependencies or side effects come into play.


Too true.....

Have you ever used a hardware descriptor language like AHDL or VHDL? Looks a
bit like Pascal, but compiles the code to yield logic gates in silicon. Thus

IF A == cat THEN
OP = Vcc;
ELSE
OP = Gnd;
END IF;

Though written sequentially, the behaviour it describes is simultaneously
'in place'. The problem with implementation of logic via a sequential
machine, ie a processor, is just that, sequential, and you need to
understand how the compiler will construct that sequence.

Dave



Back to top
Nicolai Hansen
Guest





PostPosted: Sun Nov 16, 2003 5:15 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

Quote:
Have you ever used a hardware descriptor language like AHDL or VHDL? Looks
a
bit like Pascal, but compiles the code to yield logic gates in silicon.
Thus


Fun that you mention it .. One of our hardware engineers (or electronic
engineer, or
something just like that) is trying to learn me VHDL at the moment.

Fear, cause soon even weirder ideas will pop in my twisted mind :D




Back to top
Martin Harvey (Demon Acco
Guest





PostPosted: Mon Nov 17, 2003 2:30 am    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

On Sun, 16 Nov 2003 12:52:10 GMT, "David Reeve"
<dree4456 (AT) big-pond (DOT) net.au> wrote:

Quote:
Have you ever used a hardware descriptor language like AHDL or VHDL? Looks a
bit like Pascal, but compiles the code to yield logic gates in silicon. Thus

IF A == cat THEN
OP = Vcc;
ELSE
OP = Gnd;
END IF;

Though written sequentially, the behaviour it describes is simultaneously
'in place'. The problem with implementation of logic via a sequential
machine, ie a processor, is just that, sequential, and you need to
understand how the compiler will construct that sequence.

Yep, I used Verilog for a bit - programmed some Xilinx FPGA's with it.
Very cool - we had some nice software which would take the verilog,
compile it, do all the placing / routing / whatever, and the translate
it into counters, gates etc that then got programmed into the chip.

Very neat, but you have to be careful if you're a software programmer
- the apparent "sequentiality" is of course false - although you can
tell it which bits you want clocked, and which bits you don't.

Kinda cool: All the statements in your "program" execute
simultaneously ;-)

<Insert manic discussion about clocked versus asynchronous logic here>

MH.


Back to top
David Reeve
Guest





PostPosted: Mon Nov 17, 2003 6:24 am    Post subject: Re: something I came to think about ... (logical operators i Reply with quote


"Nicolai Hansen" <nic (AT) aub (DOT) dk> wrote

Quote:
Have you ever used a hardware descriptor language like AHDL or VHDL?
Looks
a
bit like Pascal, but compiles the code to yield logic gates in silicon.
Thus

Fun that you mention it .. One of our hardware engineers (or electronic
engineer, or
something just like that) is trying to learn me VHDL at the moment.

Fear, cause soon even weirder ideas will pop in my twisted mind :D




Not only is this stuff really good fun, the results are commercially potent.
Something that doesn't happen every day Smile
You'll enjoy yourself provided you don't lose sight of what it means in
hardware terms. Thus A = B, takes a wire from the output of object B and
connects it to the input of object A. Its that simple, and that abstract!

Dave



Back to top
David Reeve
Guest





PostPosted: Mon Nov 17, 2003 7:36 am    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

"Martin Harvey (Demon Account)" <martin (AT) pergolesi (DOT) demon.co.uk> wrote in
message news:5gcgrvsuab3d5srbtifqfq15oh351aks97 (AT) 4ax (DOT) com...
Quote:

[snip]
Insert manic discussion about clocked versus asynchronous logic here


We're in danger of running way OT here, but might be excused because it does
bear upon the original topic of how semantics ultimately must map onto the
real world, warts and all. Anyway, my excuse is that I find programming
languages so unreasonably exciting Smile The hardware descriptor languages
are extreme in this regard, and amaze and delight in the way hardware can be
instantiated by means of such a remote and abstract mapping...... not a
soldering iron, or bit of Vero board in sight.

A little sample.... a preloadable up/down counter....note the distinction
between simultaneous and sequential is not made using any keyword but is
derived purely from the semantic context. And this is a very simple example.
Things get much more complex, but still work very well. Like with Delphi,
the trick it seems is to develop a feeling for the underlying ethos and work
with it. It's not good to try to outsmart the compiler.

Dave


(
clk : INPUT; -- declares ins and outs
d[11..0] : INPUT;
countup : INPUT;
clear : INPUT;
preset : INPUT;
load : INPUT;
enctr : INPUT = Vcc;
q[11..0] : OUTPUT;
)


VARIABLE -- just a var block
reg[11..0] : DFF;

BEGIN -
DEFAULTS
reg[].clrn = VCC;
reg[].prn = VCC;
END DEFAULTS;

reg[].clk = clk; -- wires the dflops
q[] = reg[];

!reg[].prn = preset OR (load & d[]); -- wires in the load functionality
!reg[].clrn = clear OR (load & !d[]); -- this behaviour is asynchronous
ie always in place

IF countup & enctr THEN -- describes up/down count
behaviour
reg[] = reg[] + 1; -- this behaviour occurs
only on the rising edge of the clock
ELSIF !countup & enctr THEN -- ie synchronous logic
reg[] = reg[] - 1;
ELSE
reg[] = reg[];
END IF;
END.





Back to top
Maarten Wiltink
Guest





PostPosted: Mon Nov 17, 2003 1:05 pm    Post subject: Re: something I came to think about ... (logical operators i Reply with quote

"Martin Harvey (Demon Account)" <martin (AT) pergolesi (DOT) demon.co.uk> wrote in
message news:5gcgrvsuab3d5srbtifqfq15oh351aks97 (AT) 4ax (DOT) com...
[...]
Quote:
[Verilog, a hardware description languages is] Very neat, but you
have to be careful if you're a software programmer - the apparent
"sequentiality" is of course false - although you can tell it which
bits you want clocked, and which bits you don't.

SEQ
x=y
y=x

PAR
x=y
y=x

_Almost_ a hardware description language (and being abused here, yes).

Groetjes,
Maarten Wiltink



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.