comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Exception contracts for Ada?
Date: Tue, 25 Dec 2012 12:09:12 +0100
Date: 2012-12-25T12:09:12+01:00	[thread overview]
Message-ID: <b1pa78w9upjm.sso5n80uauzp.dlg@40tude.net> (raw)
In-Reply-To: XYadnZrAFoJvLUXN4p2dnAA@giganews.com

On Mon, 24 Dec 2012 14:45:53 -0500, Peter C. Chapin wrote:

> I know there be dragons down this road, but I will comment on a few of 
> your points. First, I want to emphasize that I'm not necessarily 
> disagreeing with you... just elaborating on some ideas.
> 
> On 12/24/2012 11:34 AM, Dmitry A. Kazakov wrote:
> 
>>> + What about backward compatibility with the existing code base?
>>
>> No problem at all. Empty (missing) contract = any exception may propagate.
>> Everything would be 100% compatible.
> 
> But is that what we would want? I'm not sure.

It must be so, even if the language were developed from scratch. The
problem is that exception checks are evidently undecidable. It means that
you must have a grey zone for the things you cannot prove either true or
false.

Of course some legacy stuff could be much better if contracted. E.g. a
promise not to raise anything in Finalize/Initialize, but that would indeed
break legacy code.
 
>>> + Should exception contracts distinguish between "impossible" exceptions
>>> that shouldn't occur in a correct program (like Constraint_Error) and
>>> "normal" exceptions that pertain to environmental problems such as
>>> invalid input data?
>>
>> There is no such distinction.
> 
> Java does not require exception specifications for certain classes of 
> exceptions such as for dereferencing null pointers and similar low level 
> (language support) things. If it did then virtually every method would 
> need to be decorated with an exception specification.
> 
> In Ada there are, for example, so many ways Constraint_Error can be 
> raised that requiring it to be contracted by every subprogram seems... 
> wrong somehow.

But a program is not required to have a specific exception contract. The
language should not enforce that. The default contract is: I can raise
anything.

>>> + How should exception contracts interact with generic code?
>>
>> As any other language thing does. Generic code has "generic contracts"
>> which become normal contracts after instantiation.
> 
> The interesting part is not the contracts on the generic code itself but 
> rather on the entities used by the generic code. Are exception contracts 
> on generic code to be checked when the generic code is compiled or at 
> instantiation time?

Yes

> Will it be necessary to add syntax so that generic 
> formal parameters can have their exception contracts declared?

Possibly, e.g. formal exception, formal set of exceptions as generic
parameters.
 
> I suppose there are similar questions surrounding the interaction of 
> pre- and postconditions and generic code. I'm not sufficiently up on Ada 
> 2012 yet to know how those issues are currently handled but I'm guessing 
> that, because they are dynamic checks, the compiler doesn't worry about 
> them.

Sure, any [object] language aspect may have a generic counterpart [in the
meta language].

That should rather bother people pushing for more generics. To me it makes
little sense to invest any efforts into further developing of obviously
lousy concept.

>>> + Should exception contracts be a part of a subprogram's type?
>>
>> There is no subprogram types in Ada so far.
> 
> If I call a subprogram indirectly via an access value, what do I know 
> statically about its exception contract? How can I statically check that 
> the exception contracts are being honored?

This looks simple. When you declare an access to subprogram type, that
declaration will have an exception contract (possibly empty). E.g.

   type P is access procedure ... with exceptions => E1;

E1 is an exception contract. Now let you declare

   procedure F ... with exceptions => E2;

Then F'Access matches P if parameters do and E2 implies E1. Since contracts
are statically checked, you can check that at compile time.

[ A very useful thing for bindings, preventing passing Ada callbacks
raising exceptions to C libraries ]

> I do know that, in GNAT at least, preconditions can't be declared for 
> access to subprogram types.

For dynamic checks there is little difference anyway. I suppose 

>> Do you mean the case when P calls to Q which is allowed to propagate E
>> while P promises not to propagate E? In that case if analysis shows that P
>> never propagates E, no special handler is required.
> 
> Yes that's basically what I mean. What you are suggesting sounds like it 
> would require mandating a certain level of static analysis in compilers.

Yes, that is the most difficult part of it, specification what is required
to be provable about exceptions = anything else is mandated to be
considered non-provable, even if you or compiler knows otherwise.

Although exception checks could be very, very conservative. I don't think
anything more sophisticated than what Ada mandates, for example, for
presence of return statements in the function body is required.

Static analysis could simply look after all calls the body does and merge
their contracts [so for any block having handlers]. One could refine this a
bit towards if- and case-statements with static expressions, but that is
not really needed, IMO.

> It seems to me that's a case where the standard is actively interfering 
> with the capabilities of an advanced compiler.

The language, especially its type system, is full of such things. When you
do

   type T is new Integer;
 
then T is treated as another type, though you and the compiler know them
same.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2012-12-25 11:09 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-18  7:45 Press Release - Ada 2012 Language Standard Approved by ISO Dirk Craeynest
2012-12-18 16:57 ` Robert A Duff
2012-12-18 21:12   ` Bill Findlay
2012-12-18 21:36     ` Jeffrey Carter
2012-12-18 21:57       ` Bill Findlay
2012-12-19  8:33     ` Dmitry A. Kazakov
2012-12-19  9:00       ` Georg Bauhaus
2012-12-19  9:19         ` Dmitry A. Kazakov
2012-12-19  9:21           ` Georg Bauhaus
2012-12-19  9:38             ` Dmitry A. Kazakov
2012-12-19 12:23               ` Georg Bauhaus
2012-12-19 14:34       ` Bill Findlay
2012-12-20  1:52       ` Randy Brukardt
2012-12-21  9:01         ` Dmitry A. Kazakov
2012-12-21 10:13           ` Georg Bauhaus
2012-12-21 10:34             ` Georg Bauhaus
2012-12-21 13:38             ` Dmitry A. Kazakov
2012-12-22  1:40           ` Randy Brukardt
2012-12-22  9:02             ` Dmitry A. Kazakov
2012-12-22 22:38               ` Georg Bauhaus
2012-12-23  8:08                 ` Dmitry A. Kazakov
2012-12-23 23:38                   ` Shark8
2012-12-24  2:44                     ` sbelmont700
2012-12-24  5:29                       ` Shark8
2012-12-25 21:51                       ` Florian Weimer
2012-12-27  1:00                         ` sbelmont700
2012-12-27  1:47                           ` Randy Brukardt
2012-12-27 14:29                             ` sbelmont700
2012-12-27 15:30                               ` Dmitry A. Kazakov
2012-12-27 18:48                                 ` Jeffrey Carter
2012-12-27 21:54                                   ` Randy Brukardt
2012-12-27 22:09                                     ` J-P. Rosen
2013-01-11 11:41                                     ` Yannick Duchêne (Hibou57)
2013-01-11 11:35                                   ` Yannick Duchêne (Hibou57)
2013-01-11 16:13                                     ` Jacob Sparre Andersen
2013-01-12  2:06                                       ` Randy Brukardt
2013-01-11 11:33                                 ` Yannick Duchêne (Hibou57)
2013-01-11 14:15                                   ` Dmitry A. Kazakov
2013-01-11 16:19                                 ` File_Exists (Was: Press Release - Ada 2012 Language Standard Approved by ISO) Jacob Sparre Andersen
2013-01-11 19:36                                   ` Yannick Duchêne (Hibou57)
2013-01-14  5:09                                     ` File_Exists Jacob Sparre Andersen
2013-01-12  7:55                                   ` File_Exists (Was: Press Release - Ada 2012 Language Standard Approved by ISO) Georg Bauhaus
2012-12-27 20:12                             ` compilers, was Re: Press Release - Ada 2012 Language Standard Approved by ISO tmoran
2012-12-27 20:54                               ` Shark8
2012-12-27 22:00                               ` Randy Brukardt
2012-12-27 10:05                           ` Dmitry A. Kazakov
2013-01-11 11:28                           ` Yannick Duchêne (Hibou57)
2013-01-11 14:21                             ` Dmitry A. Kazakov
2013-01-11 14:23                               ` Yannick Duchêne (Hibou57)
2012-12-24  6:44                   ` Yannick Duchêne (Hibou57)
2012-12-24  9:02                     ` Dmitry A. Kazakov
2012-12-24 11:13                       ` Yannick Duchêne (Hibou57)
2012-12-24 15:49                         ` Exception contracts for Ada? Was: " Peter C. Chapin
2012-12-24 16:34                           ` Dmitry A. Kazakov
2012-12-24 19:45                             ` Exception contracts for Ada? Peter C. Chapin
2012-12-25 11:09                               ` Dmitry A. Kazakov [this message]
2012-12-27  2:38                               ` Randy Brukardt
2012-12-27  2:16                           ` Exception contracts for Ada? Was: Re: Press Release - Ada 2012 Language Standard Approved by ISO Randy Brukardt
2012-12-27 15:03                             ` Peter C. Chapin
2012-12-27  1:17               ` Randy Brukardt
2013-01-11 17:10       ` Marius Amado-Alves
2012-12-20 21:55     ` Anh Vo
2012-12-21  1:04       ` Bill Findlay
2012-12-18 23:24   ` Randy Brukardt
2012-12-19  8:35     ` Georg Bauhaus
2012-12-19  9:03       ` Dmitry A. Kazakov
replies disabled

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