-- Purpose: This program demonstrates static and dynamic predicates
--
with ada.integer_text_io; use ada.integer_text_io;
with ada.text_io; use ada.text_io;
with Ada.Exceptions; use Ada.Exceptions;
procedure static_pred is
subtype Small_Ints1 is Natural range 0 .. 10;
subtype Small_Ints2 is Natural
with static_predicate => (Small_Ints2 in 1 .. 5 | 11 .. 15 | 99)
or Small_Ints2 > 1_000
or Small_Ints2 = 10_000;
subtype Odd is Integer
with dynamic_predicate => Odd mod 2 = 1;
subtype Even is Integer
with dynamic_predicate => Even not in Odd;
-- Caution: Computationally intensive!
subtype Primes is Positive range 2 .. Positive'Last
with dynamic_predicate =>
(for all i in 2 .. primes / 2 => primes mod i /= 0);
-- Caution: Computationally intensive!
subtype Composite is Positive range 4 .. Positive'Last
with dynamic_predicate =>
(for some i in 4 .. Composite / 2 => Composite mod i = 0);
s : Small_Ints2;
begin
loop
begin
put ("Enter int: ");
get(s);
put(s);
new_line;
exception
when e: others =>
put_line (Exception_Name (e) & " " & Exception_Message (e));
end;
end loop;
end static_pred;