comp.lang.ada
 help / color / mirror / Atom feed
From: Bob Duff <bobduff@theworld.com>
Subject: Re: SPARK: missing case value
Date: Fri, 09 Oct 2015 10:39:01 -0400
Date: 2015-10-09T10:39:01-04:00	[thread overview]
Message-ID: <87twq09ke2.fsf@theworld.com> (raw)
In-Reply-To: ad7b2827-9111-4d9b-b065-1ebd26795365@googlegroups.com

Maciej Sobczak <see.my.homepage@gmail.com> writes:

> Consider:
>
>    type Enum is (A, B, C);
>
>    procedure Test (E : in Enum)
>       with Pre => E /= C
>    is
>    begin
>       case E is
>          when A => null;
>          when B => null;
>       end case;
>    end Test;

That's illegal Ada, as you noted.  And illegal SPARK.

But this works:

   type Enum is (A, B, C);
   subtype A_C is Enum with Predicate => A_C /= B;

   procedure Test (E : in A_C) is
   begin
      case E is
         when A => null;
         -- "when B" is not needed.
         when C => null;
      end case;
   end Test;

And that has the advantage that A_C need not be a subrange;
it can have holes.

I find that predicates are often better than preconditions,
because the same precondition often applies to many parameters,
and also to local variables.  The predicate allows you to avoid
repetition.

("Predicate =>" is a GNAT-specific extension.  In Ada, you need to
say "Static_Predicate =>".  IMHO the "Static_" part is just noise,
but I couldn't convince the rest of ARG.)

- Bob


  parent reply	other threads:[~2015-10-09 14:39 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-09 11:38 SPARK: missing case value Maciej Sobczak
2015-10-09 12:28 ` Stefan.Lucks
2015-10-09 12:35 ` Mark Lorenzen
2015-10-09 14:53   ` Bob Duff
2015-10-09 14:39 ` Bob Duff [this message]
2015-10-09 15:10   ` Dmitry A. Kazakov
2015-10-09 15:22     ` Bob Duff
2015-10-09 15:34       ` Dmitry A. Kazakov
2015-10-09 16:20     ` G.B.
2015-10-09 16:35       ` Dmitry A. Kazakov
2015-10-09 20:29         ` Georg Bauhaus
2015-10-09 21:01           ` Dmitry A. Kazakov
2015-10-10  6:44             ` Randy Brukardt
2015-10-10  9:10             ` Georg Bauhaus
2015-10-10 10:00               ` Dmitry A. Kazakov
2015-10-10 14:19                 ` Georg Bauhaus
2015-10-11  9:49                   ` 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