comp.lang.ada
 help / color / mirror / Atom feed
From: Stefan.Lucks@uni-weimar.de
Subject: Re: {Pre,Post}conditions and side effects
Date: Thu, 7 May 2015 20:00:54 +0200
Date: 2015-05-07T20:00:54+02:00	[thread overview]
Message-ID: <alpine.DEB.2.11.1505071909280.16448@debian> (raw)
In-Reply-To: <121ytg16vbxsk$.18dxo1h7daxty.dlg@40tude.net>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 3647 bytes --]

On Thu, 7 May 2015, Dmitry A. Kazakov wrote:

>> Here, I heavily disagree. Often, checking relevant properties is much to
>> expensive to perform the checks them in production code.
>
> Merely a confusion of checks with code. There is no such thing as runtime
> correctness check,

Agreed!

At best, a run time check is some sort of a testing harness. It is useless 
to show the correctness of a program, but it can help to discover bugs.

>> A simple example is binary search over a sorted array. The precondition
>> requires the array to be sorted.
>
> And what does it mean for the behavior? If unsorted input is *valid* and
> *must* raise exception (contracted behavior) then you cannot remove code
> *implementing* this behavior.
>
> Consider this client program:

[...] I'll slightly rewrite your client program -- I believe, the 
exception handler was at the wrong place.

   declare
     X : Extended_Index_Type;
   begin
     X := Search (Data, Key);
   exception
       when Not_Sorted_Error | Assertion_Error =>
          X := Search (Sort (Data), Key);
   end;

> Is this code correct?

It depends on the specification of "Sort".

Specification 1:

   function Search(Data: Array_Type; Key: Value_Type)
                      return Extended_Index_Type with
     pre  => Sorted(Data),
     post => (if Data(Search'Result) in Data'Range
              then Data(Search'Result)=Value
              else Search'Result = No_Index and then
                   (for all I in Data'Range => Data(I) /= Key));

The expression "Sorted(Data)" is a precondition. Every client which can 
possibly violate the precondition is buggy. Thus, the above code is buggy. 
(Or otherwise, the exception handler is dead code.)

One property of a proper precondition (or postcondition or ...) is that 
disabling the check does not change the functional behaviour of correct 
programs.

Specification 2:

   function Search(Data: Array_Type; Key: Value_Type)
                      return Extended_Index_Type with
     pre  => (Sorted(Data) or else raise Not_Sorted_Error),
     post => (if Data(Search'Result) in A'Range
              then Data(Search'Result)=Value
              else Search'Result = No_Index and then
                   (for all I in Data'Range => Data(I) /= Key));

The expression following "pre" is "contracted behaviour" as you put it. 
The code above is correct, and disabling the check must be prohibited, 
because it would break correct programs. Which is why I wrote the following:

>> Most urgently, I would expect an option to skip checking ordinary pre- and
>> postconditions, without skipping those that explicitly raise some
>> exceptions.

In other words, I want to be able to switch of proper preconditions (and 
postconditions, whatever) without affecting contracted behaviour.

I depreciate the usage of the word "precondition" for the expression 
following "pre" in spec 2. But I will not fight about names.

Furthermore, I am quite happy with Ada allowing to specify contracted 
behaviour, even I would have prefered use an aspect of its own for 
contraced behaviour. The "pre" aspect should better be have been reserved 
for proper preconditions. But this appears too late now. On the other 
hand, it would not be too late to support disabling proper preconditions 
without changing contracted behaviour.

Stefan


------  I  love  the  taste  of  Cryptanalysis  in  the morning!  ------
uni-weimar.de/de/medien/professuren/mediensicherheit/people/stefan-lucks
--Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universität Weimar, Germany--

  reply	other threads:[~2015-05-07 18:00 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 [this message]
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
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