--  In an instance, a formal derived type names its ancestor's components, discriminants its
--  actual renamed or hid among them (RM 12.5.1(21/3), ACATS CC51003, CC51004); `others` in a
--  record aggregate over components of different discriminant constraints; the rows of a
--  rectangular aggregate within a discriminant's bounds, positional or named with `others`
--  (D257).
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   type Matrix is array (Natural range <>, Natural range <>) of Integer;
   type Square (Side : Natural) is record
      Mat : Matrix (1 .. Side, 1 .. Side);
   end record;
   type Double_Square (Number : Natural) is record
      Left  : Square (Number);
      Right : Square (Number);
   end record;
   type Sq_Type (Num1 : Natural) is tagged record
      One : Square (Num1);
   end record;
   type New_Dbl_Sq (Num2 : Natural) is new Sq_Type (Num2) with record
      Two : Square (Num2);
   end record;
   type New_Double (Num : Natural) is new Double_Square (Num);
   type Pair (A, B : Natural) is record
      X : Square (A);
      Y : Square (B);
   end record;

   generic
      type Dbl is new Double_Square;
   function Size_Of (D : Dbl) return Natural;
   function Size_Of (D : Dbl) return Natural is (D.Number);

   generic
      type Squares is new Sq_Type with private;
   function First_Of (S : Squares) return Integer;
   function First_Of (S : Squares) return Integer is
     (if S.One.Mat'Length (1) > 0 and S.One.Mat'Length (2) > 0 then S.One.Mat (S.One.Mat'First (1), S.One.Mat'First (2)) else S.Num1);

   function Size_Root is new Size_Of (Double_Square);
   function Size_New is new Size_Of (New_Double);
   function First_Root is new First_Of (Sq_Type);
   function First_New is new First_Of (New_Dbl_Sq);

   M3 : constant Square (Side => 3) := (Side => 3, Mat => (1 => (1, 4, 9), others => (1 => 5, others => 7)));
   M4 : constant Square (Side => 3) := (Side => 3, Mat => ((2, others => 8), others => (6, 6, others => 3)));
   Z : constant Square (2) := (2, Mat => ((0, 1), (2, 3)));
   D : constant Double_Square (3) := (Number => 3, others => M3);
   E : constant New_Double (2) := (Num => 2, others => Z);
   Q : constant Sq_Type (3) := (Num1 => 3, One => M4);
   N : constant New_Dbl_Sq (2) := (2, others => Z);
   P : constant Pair (2, 2) := (2, 2, others => Z);
begin
   pragma Assert (M3.Mat (3, 1) = 5 and M3.Mat (2, 2) = 7);
   Put_Line (Integer'Image (M3.Mat (1, 3)) & Integer'Image (M4.Mat (1, 1)) & Integer'Image (M4.Mat (1, 3)) & Integer'Image (M4.Mat (3, 2)) & Integer'Image (M4.Mat (2, 3)));
   Put_Line (Integer'Image (Size_Root (D)) & Integer'Image (Size_New (E)));
   Put_Line (Integer'Image (First_Root (Q)) & Integer'Image (First_New (N)));
   Put_Line (Integer'Image (P.Y.Mat (2, 1)) & Integer'Image (N.Two.Mat (1, 2)) & Integer'Image (D.Right.Mat (3, 3)));
end Main;
