comp.lang.ada
 help / color / mirror / Atom feed
From: Joachim Durchholz <joachim.durchholz@munich.netsurf.de>
Subject: Re: Building blocks (Was: Design By Contract)
Date: 1997/10/01
Date: 1997-10-01T00:00:00+00:00	[thread overview]
Message-ID: <34329773.B05F348B@munich.netsurf.de> (raw)
In-Reply-To: 199710011402.QAA02444@basement.replay.com


Anonymous wrote:
> 
> Certainly "while" is preferred by those doing program correctness
> proofs; all the techniques for this that I have seen have been for
> "while" loops. Avoiding "while" does usually make for more readable
> code. In this specific example, "while" requires a flag variable,
> which is less readable than using "exit".

Actually positive/negative logic has nothing to do with why the
correctness guys prefer "while". What they want is that the termination
condition is at the start of the loop.
Actually Eiffel doesn't use "while", it uses "until". The correct syntax
is:

from
  -- loop initialization - arbitrary code
invariant
  -- here goes the loop invariant, a boolean expression that's
  -- true whenever execution reaches the top of the loop.
  -- loop invariants are useful to prove the correctness.
variant
  -- here the loop variant - an expression that will decrease
  -- with every iteration.
  -- loop variants are useful to prove that the loop will terminate.
until
  -- put your termination condition here
loop
  -- statements to be repeated
end

The "from" part is just syntactic sugar - leaving away the "from"
keyword won't change the semantics. Still it's useful to indicate the
beginning of the loop set-up code - it's often rather contrived to force
the loop invariant on the first go through the loop.

"invariant" and "variant" are the algorithm prover's tools. However,
proving and convincing somebody that it actually works is much the same,
i.e. these constructs are even useful when hacking. In particular if
you're in doubt wether your loop does exactly what it should, and wether
it terminates at all! Remember your first attempts at binary search in
an array?

The above construct has interesting properties (which will be well-known
to algorithm provers):
1. If a variant is present the code is guaranteed to terminate. Quite
useful for loops.
2. The loop invariant will be true at the start and at the end of the
code of the loop body.
3. When the loop terminates, both the invariant and the termination
condition will hold. Usually you don't have to look at the loop body to
understand what the loop is doing.
4. When reading a loop without the invariant stated, the worst problem
for understanding the code is this: We know numerous conditions that are
true when execution reaches the top of the loop, but we don't know which
of these conditions will be destroyed by the loop body. So we don't
understand which conditions will still hold for the second iteration,
i.e. we don't know what conditions the code of the loop body assumes.
This makes understanding the code extremely difficult - this is much
like reading the body of a routine without being allowed to look at the
parameter and local variable declarations (actually I think it's worse).

Regards,
Joachim
-- 
Please don't send unsolicited ads. (The @@ is for confusing spambots.)






  parent reply	other threads:[~1997-10-01  0:00 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-09  0:00 Building blocks (Was: Design By Contract) Marc Wachowitz
1997-09-15  0:00 ` Joachim Durchholz
1997-09-17  0:00 ` Paul Johnson
1997-09-18  0:00   ` Robert Dewar
1997-09-18  0:00   ` Stephen Leake
1997-09-18  0:00     ` Mark L. Fussell
1997-09-19  0:00       ` Jon S Anthony
1997-09-23  0:00         ` Mark L. Fussell
     [not found]       ` <11861963wnr@eiffel.demon.co.uk>
1997-09-19  0:00         ` Mark L. Fussell
1997-09-19  0:00       ` Robert A Duff
1997-09-20  0:00         ` Joachim Durchholz
1997-09-22  0:00           ` Matthew Heaney
1997-09-23  0:00             ` Veli-Pekka Nousiainen
1997-10-03  0:00               ` Robert I. Eachus
1997-10-04  0:00                 ` Paul Johnson
1997-10-14  0:00                   ` Robert I. Eachus
1997-09-23  0:00             ` Joachim Durchholz
1997-09-23  0:00           ` Jon S Anthony
1997-09-24  0:00           ` Alan E & Carmel J Brain
1997-09-25  0:00             ` Anonymous
1997-09-30  0:00               ` Alan E & Carmel J Brain
1997-09-30  0:00                 ` Matthew Heaney
1997-09-30  0:00                   ` Neil Wilson
1997-09-30  0:00                     ` Stephen Leake
1997-09-30  0:00                   ` W. Wesley Groleau x4923
1997-09-30  0:00                     ` Matthew Heaney
1997-10-01  0:00                     ` Alan E & Carmel J Brain
1997-10-01  0:00                 ` Anonymous
1997-10-01  0:00                   ` Paul M Gover
1997-10-04  0:00                     ` Paul Johnson
1997-10-04  0:00                       ` Matthew Heaney
1997-10-15  0:00                         ` Paul Johnson
1997-10-15  0:00                           ` Matthew Heaney
1997-10-16  0:00                             ` Joachim Durchholz
1997-10-17  0:00                               ` Robert I. Eachus
1997-10-16  0:00                           ` Joachim Durchholz
1997-10-22  0:00                           ` Reimer Behrends
1997-10-01  0:00                   ` Joachim Durchholz [this message]
1997-10-02  0:00                   ` Robert A Duff
1997-10-02  0:00                     ` Tucker Taft
1997-10-02  0:00                       ` Matthew Heaney
1997-10-03  0:00                     ` Stephen Leake
1997-10-04  0:00                     ` Matthew Heaney
1997-10-07  0:00                       ` Robert A Duff
1997-09-24  0:00           ` Richard A. O'Keefe
1997-09-18  0:00     ` W. Wesley Groleau x4923
1997-09-21  0:00       ` Matthew Heaney
1997-09-18  0:00   ` Jon S Anthony
  -- strict thread matches above, loose matches on Subject: below --
1997-09-11  0:00 Robert Dewar
1997-09-09  0:00 Marc Wachowitz
1997-09-02  0:00 Design By Contract Jon S Anthony
     [not found] ` <JSA.97Sep3201329@alexandria.organon.com>
1997-09-04  0:00   ` Paul Johnson
     [not found]     ` <5un58u$9ih$1@gonzo.sun3.iaf.nl>
1997-09-06  0:00       ` Building blocks (Was: Design By Contract) Joachim Durchholz
1997-09-08  0:00       ` Paul Johnson
1997-09-08  0:00         ` Brian Rogoff
1997-09-09  0:00           ` Matthew Heaney
1997-09-09  0:00             ` W. Wesley Groleau x4923
1997-09-10  0:00               ` Robert A Duff
1997-09-12  0:00                 ` Jon S Anthony
1997-09-09  0:00             ` Brian Rogoff
1997-09-10  0:00             ` Paul Johnson
1997-09-10  0:00               ` Darren New
1997-09-10  0:00               ` Matthew Heaney
1997-09-10  0:00             ` Robert Dewar
1997-09-12  0:00               ` Paul Johnson
1997-09-14  0:00                 ` Robert Dewar
1997-09-15  0:00                   ` John G. Volan
1997-09-14  0:00                 ` Robert Dewar
1997-09-14  0:00                 ` Robert Dewar
1997-09-12  0:00               ` Jon S Anthony
1997-09-12  0:00                 ` Robert Dewar
1997-09-16  0:00                   ` Brian Rogoff
1997-09-09  0:00           ` W. Wesley Groleau x4923
1997-09-09  0:00           ` Veli-Pekka Nousiainen
1997-09-09  0:00             ` Jon S Anthony
1997-09-09  0:00           ` Veli-Pekka Nousiainen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox