--  APL in Vada (T32b): the examples of apl-variant.md 9 and classic one-liners, each an
--  expression function whose value is APL text, proved like the Ada it means.
pragma Extensions_Allowed (On);
package Arrays
  with SPARK_Mode
is
   subtype Small is Integer range -100 .. 100;
   subtype Extent is Natural range 0 .. 10;
   type Vector is array (Positive range <>) of Small;
   type Values is array (Positive range <>) of Integer;
   type Matrix is array (Positive range <>, Positive range <>) of Small;
   type Wide is array (Positive range <>, Positive range <>) of Integer;

   --  the mean, rounded down: `÷` of integers under `⌊` is floor division (AP9)
   function Mean (X : Vector) return Integer is (APL ("⌊ (+/ X) ÷ ≢ X"))
     with Pre => X'Length in 1 .. 1_000;

   function Dot (X, Y : Vector) return Integer is (APL ("+/ X × Y"))
     with Pre => X'Length = Y'Length and X'Length <= 1_000;

   function Largest (X : Vector) return Integer is (APL ("⌈/ X"));

   --  the values above a limit, in order (a compress)
   function Above (X : Vector; Limit : Small) return Values is (APL ("(X > Limit) / X"))
     with Pre => X'Length <= 1_000;

   function Running (X : Vector) return Values is (APL ("+\ X"))
     with Pre => X'Length <= 1_000;

   function Running_Peaks (X : Vector) return Values is (APL ("⌈\ X"));

   --  the alternating sum, APL's right-to-left `-/`
   function Alternating (X : Vector) return Integer is (APL ("-/ X"))
     with Pre => X'Length <= 1_000;

   function Reversed (X : Vector) return Values is (APL ("⌽ X"));

   function Rotated (X : Vector; N : Natural) return Values is (APL ("N ⌽ X"))
     with Pre => X'Length in 1 .. 1_000;

   --  the multiplication table of 1 .. N (`⍳` counts from 0)
   function Table (N : Extent) return Wide is (APL ("(1 + ⍳ N) ∘.× 1 + ⍳ N"));

   function Product (A, B : Matrix) return Wide is (APL ("A +.× B"))
     with Pre => A'Length (2) = B'Length (1)
            and A'Length (1) in Extent and A'Length (2) in Extent and B'Length (2) in Extent;

   function Transposed (M : Matrix) return Wide is (APL ("⍉ M"));

   function Row_Totals (M : Matrix) return Values is (APL ("+/ M"))
     with Pre => M'Length (2) in Extent;

   function Column_Totals (M : Matrix) return Values is (APL ("+⌿ M"))
     with Pre => M'Length (1) in Extent;

   --  the rows of a vector of six, three by two, then each row's total (the rank operator)
   function Pairs (X : Vector) return Values is (APL ("(+/)⍤1 ⊢ 3 2 ⍴ X"))
     with Pre => X'Length = 6;

   --  a local name and a dfn: the sum of squares less the square of the sum's mean part
   function Spread (X : Vector) return Integer is (APL ("sq ← {⍵ × ⍵} ⋄ (+/ sq X) - ≢ X"))
     with Pre => X'Length <= 100;
end Arrays;
