comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: {Pre,Post}conditions and side effects
Date: Thu, 7 May 2015 13:52:25 -0500
Date: 2015-05-07T13:52:25-05:00	[thread overview]
Message-ID: <migc9a$jp8$1@loke.gir.dk> (raw)
In-Reply-To: alpine.DEB.2.11.1505071103310.14859@debian

<Stefan.Lucks@uni-weimar.de> wrote in message 
news:alpine.DEB.2.11.1505071103310.14859@debian...
...
>> I don't believe that checks in Ada (of any kind) should ever be turned 
>> off.
>
>Here, I heavily disagree. Often, checking relevant properties is much to
>expensive to perform the checks them in production code.
>
>A simple example is binary search over a sorted array. The precondition
>requires the array to be sorted. If the compiler succeeds in optimising
>the test away, it is equivalent to a static program verifier proving the
>precondition holds when the binary search is called.

Exactly! That's the entire idea; the compiler *should* be doing these 
optimizations, indeed one major branch of static program verification comes 
from enhancing compiler optimizer technology (CodePeer is an example of 
that). I think that technology should simply have stayed in the compiler.

>If the compiler fails to optimise the check away, the execution time goes 
>up
>from logarithmic to linear. If you can live with that, you don't need 
>binary search!

If the compiler fails to optimize the check away, your program is wrong in 
some sense, and you should have gotten an error or warning (depending on the 
compiler mode and exception contracts) to that effect. You ought to fix your 
program (probably by adding an appropriate predicate) so that the check 
*can* be eliminated (or pushed to somewhere where the cost is irrelevant).

>Turning off the checks just hides the problem. There is no value to that, 
>IMHO.
>The semantic of a plain precondition is essentially:
>
>   * If I a True, the postcondition will hold.
>     Never call the subprogram if I am false!!!
>     Otherwise, the subprogram might do anything.

"anything" => erroneous.

>This is a precondition in the "Design by Contract" sense.

Ada 2012 does not have these sorts of preconditions. (We've discussed it 
numerous times.) The theory is that adding a precondition NEVER makes a 
program less safe. Ergo, preconditions can be ignored, but violating an 
ignored precondition is not erroneous in the sense that violating a 
suppressed check is. There's a reason that we didn't call this "suppressing" 
preconditions! Thus, there is no circumstance where the "subprogram might do 
anything".

I've thought a bit about having some additional Assertion_Policy "suppress" 
that would allow the semantics you describe above, but we haven't talked 
about that at the language level.

> A typical example for this would be "the array is sorted" when calling
> binary search. If you call a binary search routine with an unsorted array,
> you will likely get false results. And you deserve the blame!

That's not "anything". That's a well-defined (but useless) result. Be 
careful about your terminology!

> The semantic of a precondition with something like "else raise
> This_Exception" is
>
>   * If I am True, the postcondition will hold
>     when the subprogram terminates.
>     If I am False, the subprogram will not do anything,
>     except for raising This_Exception.

This the meaning of all Ada 2012 preconditions. "This_Exception" just 
defaults to "Assertion_Error".

Anyway, the Assertion_Policy can be changed locally, and the policy in 
effect at the point of the declaration determines what policy is used for 
calls. Plus the policy can be set separately for different kinds of 
assertions. Thus, you can get the effect you want with the existing 
policies, so long as you don't try to write two different kinds of 
assertions on the same subprogram.

Note that there is some debate about the value of the fine-grained policy 
setting, it's unclear that GNAT implements it correctly. If some of their 
customers showed concern about the correct implementation of those rules, 
that certainly would change.

                                        Randy.




  parent reply	other threads:[~2015-05-07 18:52 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-22 16:22 {Pre,Post}conditions and side effects Jean François Martinez
2014-12-22 17:18 ` Brad Moore
2014-12-23  8:22   ` Jean François Martinez
2014-12-23 17:05     ` Robert A Duff
2014-12-23 21:09       ` Jean François Martinez
2014-12-23 21:35         ` Robert A Duff
2014-12-23 23:02         ` Peter Chapin
2014-12-24  1:03           ` Robert A Duff
2015-04-24  8:59             ` Jacob Sparre Andersen
2015-04-24  9:18               ` J-P. Rosen
2015-04-24 23:39                 ` Randy Brukardt
2015-04-24 12:10               ` G.B.
2015-04-24 14:40                 ` Jacob Sparre Andersen
2015-04-24 16:29                   ` G.B.
2015-04-24 23:46                   ` Randy Brukardt
2015-04-24 22:26               ` Peter Chapin
2015-04-25  0:13                 ` Randy Brukardt
2015-04-25  1:01                   ` Peter Chapin
2015-04-25  5:51                 ` Dmitry A. Kazakov
2015-04-25  0:31               ` Bob Duff
2015-04-25 12:08                 ` vincent.diemunsch
2015-04-25 16:37                   ` Georg Bauhaus
2015-05-06 21:07                   ` Randy Brukardt
2015-05-06 22:10                     ` Paul Rubin
2015-05-07  9:01                     ` Georg Bauhaus
2015-05-07  9:12                       ` Dmitry A. Kazakov
2015-05-07  9:29                         ` Georg Bauhaus
2015-05-07  9:31                           ` Georg Bauhaus
2015-05-07 18:32                         ` Randy Brukardt
2015-05-08  7:50                           ` Dmitry A. Kazakov
2015-05-08 23:31                             ` Randy Brukardt
2015-05-09  6:16                               ` Dmitry A. Kazakov
2015-05-12  0:28                                 ` Randy Brukardt
2015-05-12  8:04                                   ` Dmitry A. Kazakov
2015-05-07 10:06                     ` Stefan.Lucks
2015-05-07 12:16                       ` Dmitry A. Kazakov
2015-05-07 18:00                         ` Stefan.Lucks
2015-05-07 19:01                           ` Randy Brukardt
2015-05-07 19:29                             ` Niklas Holsti
2015-05-08 23:16                               ` Randy Brukardt
2015-05-09  5:18                                 ` Niklas Holsti
2015-05-12  0:15                                   ` Randy Brukardt
2015-05-07 19:55                             ` Dmitry A. Kazakov
2015-05-08 23:24                               ` Randy Brukardt
2015-05-09  5:47                                 ` Dmitry A. Kazakov
2015-05-07 18:52                       ` Randy Brukardt [this message]
2015-05-07 19:40                         ` Stefan.Lucks
2015-05-08  7:28                           ` Dmitry A. Kazakov
2015-05-08 22:58                             ` Randy Brukardt
2015-05-08 22:52                           ` Randy Brukardt
2015-05-09  0:14                             ` Paul Rubin
2015-05-12  0:30                               ` Randy Brukardt
2015-05-12 18:10                                 ` Paul Rubin
2015-05-12 22:01                                   ` Randy Brukardt
2015-05-13  9:35                                     ` Dmitry A. Kazakov
2015-05-13 11:53                                       ` G.B.
2015-05-13 12:47                                         ` Dmitry A. Kazakov
2015-05-13 14:06                                           ` G.B.
2015-05-13 14:21                                             ` Dmitry A. Kazakov
2015-05-13 16:33                                               ` G.B.
2015-05-13 19:15                                                 ` Dmitry A. Kazakov
2015-05-14  1:36                                                   ` Randy Brukardt
2015-05-14  7:10                                                     ` Dmitry A. Kazakov
2015-05-14  1:32                                         ` Randy Brukardt
2015-05-14  7:19                                           ` Dmitry A. Kazakov
2015-05-12  0:36                               ` Randy Brukardt
2015-05-11 10:35                             ` Stefan.Lucks
2015-05-11 21:49                               ` vincent.diemunsch
2015-05-11 22:49                                 ` Peter Chapin
2015-05-12  4:49                                   ` vincent.diemunsch
2015-05-12 23:25                                     ` Peter Chapin
2015-05-13  9:00                                       ` vincent.diemunsch
2015-05-12  4:42                                 ` vincent.diemunsch
2015-05-12 14:53                                   ` johnscpg
2015-05-13  9:14                                     ` vincent.diemunsch
2015-05-12  1:03                               ` Randy Brukardt
2015-05-12  7:21                                 ` Georg Bauhaus
2015-05-12 22:08                                   ` Randy Brukardt
2015-05-12  8:02                                 ` Georg Bauhaus
2015-05-12 22:14                                   ` Randy Brukardt
2015-05-12  8:37                                 ` Stefan.Lucks
2015-05-12 11:25                                   ` Stefan.Lucks
2015-05-12 18:44                                     ` Paul Rubin
2015-05-12 20:52                                       ` Stefan.Lucks
2015-05-18  9:49                                     ` Jacob Sparre Andersen
2015-05-18 12:10                                       ` Stefan.Lucks
2015-05-19  7:46                                         ` Jacob Sparre Andersen
2015-06-09  7:55                                           ` Stefan.Lucks
2015-06-09 12:02                                             ` G.B.
2015-06-09 17:16                                               ` Stefan.Lucks
2015-05-12 18:39                                   ` Paul Rubin
2015-05-12 20:51                                     ` Stefan.Lucks
2015-05-12 14:21                                 ` Bob Duff
2015-05-12 22:37                                   ` Randy Brukardt
2015-05-13  6:58                                     ` Georg Bauhaus
2015-05-14  1:21                                       ` Randy Brukardt
2015-05-07 21:29                         ` Georg Bauhaus
2015-05-08 23:11                           ` Randy Brukardt
2015-05-08 23:19                             ` tmoran
2014-12-23 21:53   ` Florian Weimer
2014-12-24 11:41     ` Brad Moore
2014-12-22 19:38 ` sbelmont700
2014-12-22 19:59   ` Brad Moore
2014-12-22 23:46   ` Randy Brukardt
2014-12-23 10:41   ` Georg Bauhaus
2014-12-22 23:32 ` Randy Brukardt
2015-04-24 17:59   ` Shark8
replies disabled

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