--  Verada's `while` clause wherever an iterator is written: in quantified expressions and
--  reductions in contracts and assertions (over a range, an array's components in either
--  order, and a multidimensional array's), and in iterated array aggregates over a range,
--  with a constrained or an unconstrained type.
pragma Extensions_Allowed (On);
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   type V is array (Positive range <>) of Integer;
   type Three is array (1 .. 3) of Integer;
   type Grid is array (1 .. 2, 1 .. 3) of Integer;
   subtype Small is Integer range 0 .. 100;
   type Smalls is array (Positive range <>) of Small;

   --  every value before the first zero is positive
   function Leading_Positive (A : V) return Boolean is
     (for all X of A while X /= 0 => X > 0);

   procedure Show (A : V)
     with Pre => (for all X of A while X /= 0 => X > 0)
   is
   begin
      for X of A while X /= 0 loop
         Put (Integer'Image (X));
      end loop;
      New_Line;
   end Show;

   --  the sum of the values before the first zero
   function Leading_Sum (A : Smalls) return Integer is
     ([for X of A while X /= 0 => X]'Reduce ("+", 0))
     with Pre => A'Length <= 1000,
          Post => (if A'Length = 0 then Leading_Sum'Result = [for X of A while X /= 0 => X]'Reduce ("+", 0));

   A : constant V := [3, 5, 0, -1];
   M : constant Grid := [[1, 2, 7], [0, -4, 5]];
   Firsts : constant V := [for I in 1 .. 6 while I < 4 => I * 10];
   Fixed : constant Three := [for I in 1 .. 6 while I < 4 => I];
   Paired : constant V := [for I in 1 .. 3, X of A => X * I];
begin
   pragma Assert (for all X of A while X /= 0 => X > 0);
   pragma Assert (not (for all X of A => X > 0));
   pragma Assert (for some X of A while X /= 0 => X = 5);
   pragma Assert (not (for some X of A while X /= 0 => X = -1));
   pragma Assert (for all I in reverse A'Range while A (I) /= 0 => A (I) < 0);
   pragma Assert (for all I in 1 .. 10 when I mod 2 = 0 while I < 6 => I in 2 | 4);
   pragma Assert (for all X of M while X /= 0 => X > 0);
   pragma Assert (not (for all X of M while X /= -4 => X > 0));
   Show (A);
   Put_Line (Boolean'Image (Leading_Positive (A)) & " " & Boolean'Image (Leading_Positive ([1, -2, 0])));
   Put_Line (Integer'Image (Leading_Sum ([3, 5, 0, 9])));
   for X of Firsts loop
      Put (Integer'Image (X));
   end loop;
   for X of Fixed loop
      Put (Integer'Image (X));
   end loop;
   for X of Paired loop
      Put (Integer'Image (X));
   end loop;
   New_Line;
end Main;
