comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: {Pre,Post}conditions and side effects
Date: Mon, 22 Dec 2014 17:46:28 -0600
Date: 2014-12-22T17:46:28-06:00	[thread overview]
Message-ID: <m7aagl$5q9$1@loke.gir.dk> (raw)
In-Reply-To: c17f6550-1cc8-4ef7-8477-4512d16d294a@googlegroups.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3783 bytes --]

<sbelmont700@gmail.com> wrote in message 
news:c17f6550-1cc8-4ef7-8477-4512d16d294a@googlegroups.com...
On Monday, December 22, 2014 11:22:29 AM UTC-5, Jean François Martinez 
wrote:
>> Perhaps it would have been a good idea to have a No_Side_Effects aspect 
>> ...

>It's more than just global variables, though.  Your condition might be 
>thusly ludicrous:
>
>function P return Boolean is
>   x : Some_Ptr_Type := new Integer'(42);

The storage pool of Some_Ptr_Type is a global variable. It would have to be 
mentioned in the Global aspect, else this allocator is illegal.

>begin
>   return True;
>exception
>   when others => Some_Task.Blocking_Rendezous;

There's also an aspect "Potentially_Blocking" under development, to cover 
this case.

>end P;

>Now you've got a function that doesn't access any global variables, but not 
>only randomly
>exhaust the heap,

As noted, the heap is a global variable, so that couldn't happen with Global 
=> null.

>but also permanently blocks the thread, so you are right back where you 
>started.

That's already a Bounded Error (see 11.4.2(23.1/3)). One presumes that if 
pragma Detect_Blocking is used, it will always raise Program_Error. Plus 
there would be the aspect noted above, for static checking.

>You end up needing all the same rules and restrictions as pragma Pure, 
>which means
>you might as well just be beholden to pragma Pure to begin with (or use 
>qualified
>expressions).

Actually, you need quite a bit more that Pure. Pure allows dereferences of 
access values, which can't be allowed willy-nilly (the pool is a global 
variable, after all). Plus Pure only applies to complete packages, which is 
way too unwieldy for assertions (typically an assertion depends on predicate 
functions defined with an ADT, we surely don't want to have to make the 
entire ADT Pure just to use assertions).

>But more to the point, there is no escaping that disabling/enabling checks 
>will cause the
>program to function differently; that is, after all, the entire point. 
>What constitutes
>'correctness' is only in the eyes of the programmer.  Ergo:

You're right here, and I've always thought that is a problem. It's one 
reason that a package author can prevent some or all assertions from being 
disabled in the package.

>procedure Some_Subprogram is null with Pre => false;
>
>procedure Insane is
>begin
>  Some_Subprogram;
>  raise Program_Error;
>exception
>  when others => Continue_Running;
>end Insane;
>
>No globals, but it still "breaks" when checks are disabled.

A much better example is an implementation that has implemented the 
containers using preconditions rather than explicit checks. For instance:

    procedure Replace_Element (Container : in out Vector;
                           Position  : in     Cursor;
                           New_Item  : in     Element_Type);
   with Pre'Class  => (if Tampering_With_Elements_Prohibited (Container)
                       then raise Program_Error) and then
                      (if Position = No_Element then raise Constraint_Error) 
and then
                      (if not Cursor_Belongs_To_Container (Container, 
Position)
                       then raise Program_Error;

If this is called with preconditions ignored, the required semantives of 
Replace_Element won't happen [because the checks for the various exceptions 
won't happen - no one is going to repeat the precondition checks in the 
body - if that was required, the precondition is worthless]. [Note: This 
precondition uses a couple of predicates that aren't defined in the Ada 2012 
containers, but should have been. Most likely, the next version of Ada will 
rewrite the containers this way, it will get rid of a lot of text in the 
Standard.]

                                      Randy.


  parent reply	other threads:[~2014-12-22 23:46 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
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 [this message]
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