--  Places inside class-wide objects through the Core step `as T` (D192): a root component
--  of a class-wide object assigned, nested, indexed, passed `in out` and borrowed by
--  `'Access` (B7005-06, -08); an extension's component through a downcast, its tag checked;
--  view conversions as `out` and `in out` actuals (SPARK-15, -16).
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   type Point is record
      X, Y : Integer := 0;
   end record;
   type Marks is array (1 .. 3) of Integer;
   type Shape is tagged record
      Origin : Point;
      Tags   : Marks := (others => 0);
      Id     : aliased Integer := 0;
   end record;
   type Circle is new Shape with record
      Radius : Integer := 1;
   end record;

   procedure Twice (N : in out Integer) with Pre => N in -1_000 .. 1_000, Post => N = 2 * N'Old is
   begin
      N := 2 * N;
   end Twice;

   procedure Set (P : not null access Integer; V : Integer) with Post => P.all = V is
   begin
      P.all := V;
   end Set;

   procedure Move (S : in out Shape'Class) with Pre => S.Origin.X = 0, Post => S.Origin.X = 6 and S.Tags (2) = 7 is
   begin
      S.Id := 1;
      S.Origin.X := 3;
      S.Tags (2) := 7;
      Twice (S.Origin.X);
      Set (S.Id'Access, 5);
   end Move;

   -- an extension's component, through a downcast whose tag check the precondition proves
   procedure Grow (S : in out Shape'Class) with Pre => S in Circle'Class and then Circle (S).Radius < 100, Post => Circle (S).Radius = Circle (S'Old).Radius + 1 is
   begin
      Circle (S).Radius := Circle (S).Radius + 1;
   end Grow;

   procedure Reset (B : in out Shape) with Post => B.Id = 0 is
   begin
      B.Id := 0;
   end Reset;

   subtype Small is Integer range 0 .. 100;
   procedure Put_42 (X : out Integer) with Post => X = 42 is
   begin
      X := 42;
   end Put_42;

   A : Shape;
   B : Circle := (Origin => (0, 4), Tags => (others => 1), Id => 0, Radius => 9);
   C : Shape'Class := Circle'(Origin => (0, 0), Tags => (others => 0), Id => 3, Radius => 2);
   N : Small := 1;
begin
   Move (A);
   Move (B);
   Grow (C);
   Grow (C);
   Reset (Shape (B));
   Put_42 (Integer (N));
   pragma Assert (B.Id = 0 and N = 42);
   Put_Line (Integer'Image (A.Id) & Integer'Image (A.Origin.X) & Integer'Image (A.Tags (2)) & Integer'Image (B.Id) & Integer'Image (B.Origin.X) & Integer'Image (B.Origin.Y) & Integer'Image (B.Tags (2)) & Integer'Image (B.Radius) & Integer'Image (Circle (C).Radius) & Integer'Image (C.Id) & Integer'Image (N));
end Main;
