comp.lang.ada
 help / color / mirror / Atom feed
From: Peter Chapin <PChapin@vtc.vsc.edu>
Subject: Re: Preconditions which anyway would be caught?
Date: Sat, 09 Aug 2014 15:21:31 -0400
Date: 2014-08-09T15:21:31-04:00	[thread overview]
Message-ID: <7vydnShDldsh6XvO4p2dnAA@giganews.com> (raw)
In-Reply-To: <ls5f3l$4t8$1@speranza.aioe.org>

On 2014-08-09 11:35, Victor Porton wrote:
> function Inverse (X: Float) is
> begin
>    return 1/X;
> end
>    with Pre => (X /= 0);
> 
> Is this precondition superfluous? (it anyway could be caught by erroneous 
> 1/X operation)

Proper syntax here is

function Inverse(X : Float)
  with Pre => (X /= 0) is
begin
   return 1/X;
end;

SPARK 2014 will attempt to prove that the precondition is satisfied at
every call site. It will also try to prove that 1/X will never entail
division by zero, which is trivial to do with the precondition present.

If you remove the precondition, SPARK will not try to prove anything
about X at the call site but it will still try to prove that 1/X does
not entail division by zero. Unfortunately there isn't enough
information to make that second proof go through so you'll end up with
an undischarged verification condition.

The precondition is a statement about the intended behavior of the
subprogram. It is, in some sense, part of the subprogram's design.
Without the precondition you are implying that it is okay to call
Inverse for any Float value at all. What is the subprogram supposed to
do when given 0.0? Raising Constraint_Error is unsightly because it
typically means the programmer made a mistake. So the version without
the precondition might more correctly be written as

function Inverse(X : Float) is
begin
   if X = 0.0. then
      raise Invalid_Domain;  -- Documented exception for this purpose.
   else
      return 1/X;
   end if;
end;

Peter


  parent reply	other threads:[~2014-08-09 19:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-09 15:35 Preconditions which anyway would be caught? Victor Porton
2014-08-09 15:40 ` mockturtle
2014-08-09 16:08 ` Jeffrey Carter
2014-08-09 19:21 ` Peter Chapin [this message]
2014-08-09 19:24   ` Peter Chapin
2014-08-09 21:04     ` Niklas Holsti
2014-08-09 22:57       ` Peter Chapin
2014-08-10 20:58         ` Niklas Holsti
2014-08-10  7:24       ` Dmitry A. Kazakov
2014-08-11  0:00         ` Shark8
2014-08-11  7:31           ` 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