From: Bill Findlay <yaldnif.w@blueyonder.co.uk>
Subject: Re: Press Release - Ada 2012 Language Standard Approved by ISO
Date: Tue, 18 Dec 2012 21:12:25 +0000
Date: 2012-12-18T21:12:25+00:00 [thread overview]
Message-ID: <CCF68CB9.22EB9%yaldnif.w@blueyonder.co.uk> (raw)
In-Reply-To: wcc8v8vti17.fsf@shell01.TheWorld.com
On 18/12/2012 16:57, in article wcc8v8vti17.fsf@shell01.TheWorld.com,
"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote:
> dirk@vana.cs.kuleuven.be. (Dirk Craeynest) writes:
>
>> Ada 2012 brings significant enhancements to Ada, most notably in the
>> area of "contract-based programming." New features here include the
>> ability to specify preconditions and postconditions for subprograms,
>> and invariants for private (encapsulated) types.
>
> Why does everybody neglect predicates, which are more important
> than pre/post/invariant?
A bit of fun with a predicate:
-- compile with gnatmake -gnat12la pn.adb
with Ada.Text_IO;
use Ada.Text_IO;
procedure pn is
type candidate is range 1 .. 2**30;
function is_prime (nr : candidate) return Boolean is
n : constant candidate := nr;
j : candidate;
begin
if n < 2 then return False; end if;
for d in candidate range 2..3 loop
if n = d then
return True;
elsif n mod d = 0 then
return False;
end if;
end loop;
j := 5; -- j = 6k-1, k = 1
loop
if n mod j = 0 then
return False;
end if;
j := j + 2; -- j = 6k+1
exit when j >= n;
if n mod j = 0 then
return False;
end if;
j := j + 4; -- j = 6k+5 = 6k'-1, k' = k+1
exit when j >= n;
end loop;
return True;
end is_prime;
-- The following exemplifies a couple of new features in Ada 2012,
-- namely predicates on subtypes,
-- and conditional expressions (redux, after Algol 60).
-- Only prime numbers can be successfully assigned to this subtype.
subtype prime_number is candidate
with Dynamic_Predicate => is_prime(prime_number);
OK : constant prime_number := 31; -- 31 is prime
KO : prime_number := 19; -- 19 is prime
begin
Put_Line(candidate'Image(OK) &
(if is_prime(OK) then " is" else " is not") & " a prime");
Flush;
KO := 32; -- 32 is NOT prime, so this should raise an exception!
Put_Line(candidate'Image(OK) &
(if is_prime(KO) then " is" else " is not") & " a prime");
Flush;
exception
when others =>
Put_Line("KO was" &
(if KO = 19 then " NOT" else " WRONGLY") & " set to 32");
Flush;
end pn;
--
Bill Findlay
with blueyonder.co.uk;
use surname & forename;
next prev parent reply other threads:[~2012-12-18 21:12 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 [this message]
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
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