--  'Reduce over sources in step in contracts (B7005-02, D192): a range or an array with
--  others in step is one fold, its sources each at its low bound plus the main one's
--  distance from its own, ending with the shortest; a loop's invariant over a prefix and a
--  postcondition over the whole are that fold, and an executable reduction's loop carries it.
pragma Extensions_Allowed (On);
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   type V is array (Positive range <>) of Integer range -100 .. 100;
   function Sum_Pairs (A, B : V) return Integer
     with Pre => A'Length <= 1000 and B'Length <= 1000 and A'First <= 1_000_000,
          Post => Sum_Pairs'Result = [for J in A'Range, Y of B => A (J) + Y]'Reduce ("+", 0)
   is
      S : Integer := 0;
   begin
      for I in A'First .. Integer'Min (A'Last, A'First + (B'Length - 1)) loop
         pragma Loop_Invariant
           (S = [for J in A'First .. I - 1, Y of B => A (J) + Y]'Reduce ("+", 0)
            and abs S <= 200 * (I - A'First));
         S := S + A (I) + B (B'First + (I - A'First));
      end loop;
      return S;
   end Sum_Pairs;
   function Dot (A, B : V) return Integer
     with Pre => A'Length <= 1000,
          Post => Dot'Result = [for X of A, Y of B => X * Y]'Reduce ("+", 0)
   is
   begin
      return [for X of A, Y of B => X * Y]'Reduce ("+", 0);
   end Dot;
begin
   Put_Line (Integer'Image (Sum_Pairs ((1, 2, 3), (10, 20, 30, 40))) & Integer'Image (Dot ((1, 2, 3), (10, 20, 30, 40))));
end Main;
