--  Ada.Real_Time (RM D.8) for tasks: a monotonic clock in nanoseconds. Clock is read
--  through the run-time; the prover treats each reading as an unknown value of Time. A Time
--  and a Time_Span are counts of nanoseconds, about 292 years either way. Adding a span to a
--  time stops at Time_First and Time_Last, so a task that adds its period forever, as the
--  periodic-task idiom does, is proved: a time that far away is never reached (the clock
--  starts from about when the machine started). The other operations on spans have
--  preconditions that keep them in range.
package Ada.Real_Time is
   type Time is private;
   type Time_Span is private;

   Time_First : constant Time;
   Time_Last : constant Time;
   Time_Span_First : constant Time_Span;
   Time_Span_Last : constant Time_Span;
   Time_Span_Zero : constant Time_Span;
   Time_Span_Unit : constant Time_Span;

   --  the clock's step: a nanosecond, the unit of Time
   Tick : constant Time_Span;
   Time_Unit : constant := 0.000_000_001;

   --  (Verada) a time and a span as their nanoseconds
   function Nanoseconds_Of (T : Time) return Long_Long_Integer
     with Global => null;
   function Span_Nanoseconds (TS : Time_Span) return Long_Long_Integer
     with Global => null;

   --  (the monotonic clock counts from about when the machine started: far below Time's last
   --  value, about 292 years)
   function Clock return Time
     with Global => null,
          Post => Nanoseconds_Of (Clock'Result) in 0 .. 4_000_000_000_000_000_000;

   --  a time a span later: exact, or Time_Last (Time_First) when it would be beyond it
   function "+" (Left : Time; Right : Time_Span) return Time
     with Global => null,
          Post => (if Span_Nanoseconds (Right) >= 0
                   then (if Nanoseconds_Of (Left) <= Long_Long_Integer'Last - Span_Nanoseconds (Right)
                         then Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Left) + Span_Nanoseconds (Right)
                         else Nanoseconds_Of ("+"'Result) = Long_Long_Integer'Last)
                   else (if Nanoseconds_Of (Left) >= Long_Long_Integer'First - Span_Nanoseconds (Right)
                         then Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Left) + Span_Nanoseconds (Right)
                         else Nanoseconds_Of ("+"'Result) = Long_Long_Integer'First));
   function "+" (Left : Time_Span; Right : Time) return Time
     with Global => null,
          Post => Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Right + Left);

   --  a time a span earlier, likewise
   function "-" (Left : Time; Right : Time_Span) return Time
     with Global => null,
          Post => (if Span_Nanoseconds (Right) <= 0
                   then (if Nanoseconds_Of (Left) <= Long_Long_Integer'Last + Span_Nanoseconds (Right)
                         then Nanoseconds_Of ("-"'Result) = Nanoseconds_Of (Left) - Span_Nanoseconds (Right)
                         else Nanoseconds_Of ("-"'Result) = Long_Long_Integer'Last)
                   else (if Nanoseconds_Of (Left) >= Long_Long_Integer'First + Span_Nanoseconds (Right)
                         then Nanoseconds_Of ("-"'Result) = Nanoseconds_Of (Left) - Span_Nanoseconds (Right)
                         else Nanoseconds_Of ("-"'Result) = Long_Long_Integer'First));

   function "-" (Left : Time; Right : Time) return Time_Span
     with Global => null,
          Pre => (if Nanoseconds_Of (Right) >= 0
                  then Nanoseconds_Of (Left) >= Long_Long_Integer'First + Nanoseconds_Of (Right)
                  else Nanoseconds_Of (Left) <= Long_Long_Integer'Last + Nanoseconds_Of (Right)),
          Post => Span_Nanoseconds ("-"'Result) = Nanoseconds_Of (Left) - Nanoseconds_Of (Right);

   function "<" (Left, Right : Time) return Boolean
     with Global => null,
          Post => "<"'Result = (Nanoseconds_Of (Left) < Nanoseconds_Of (Right));
   function "<=" (Left, Right : Time) return Boolean
     with Global => null,
          Post => "<="'Result = (Nanoseconds_Of (Left) <= Nanoseconds_Of (Right));
   function ">" (Left, Right : Time) return Boolean
     with Global => null,
          Post => ">"'Result = (Nanoseconds_Of (Left) > Nanoseconds_Of (Right));
   function ">=" (Left, Right : Time) return Boolean
     with Global => null,
          Post => ">="'Result = (Nanoseconds_Of (Left) >= Nanoseconds_Of (Right));

   --  spans: sums, differences, multiples and quotients, kept in range by their preconditions
   function "+" (Left, Right : Time_Span) return Time_Span
     with Global => null,
          Pre => (if Span_Nanoseconds (Right) >= 0
                  then Span_Nanoseconds (Left) <= Long_Long_Integer'Last - Span_Nanoseconds (Right)
                  else Span_Nanoseconds (Left) >= Long_Long_Integer'First - Span_Nanoseconds (Right)),
          Post => Span_Nanoseconds ("+"'Result) = Span_Nanoseconds (Left) + Span_Nanoseconds (Right);
   function "-" (Left, Right : Time_Span) return Time_Span
     with Global => null,
          Pre => (if Span_Nanoseconds (Right) >= 0
                  then Span_Nanoseconds (Left) >= Long_Long_Integer'First + Span_Nanoseconds (Right)
                  else Span_Nanoseconds (Left) <= Long_Long_Integer'Last + Span_Nanoseconds (Right)),
          Post => Span_Nanoseconds ("-"'Result) = Span_Nanoseconds (Left) - Span_Nanoseconds (Right);
   function "-" (Right : Time_Span) return Time_Span
     with Global => null,
          Pre => Span_Nanoseconds (Right) /= Long_Long_Integer'First,
          Post => Span_Nanoseconds ("-"'Result) = -Span_Nanoseconds (Right);
   function "abs" (Right : Time_Span) return Time_Span
     with Global => null,
          Pre => Span_Nanoseconds (Right) /= Long_Long_Integer'First,
          Post => Span_Nanoseconds ("abs"'Result) = abs Span_Nanoseconds (Right);
   function "*" (Left : Time_Span; Right : Integer) return Time_Span
     with Global => null,
          Pre => Long_Long_Long_Integer (Span_Nanoseconds (Left)) * Long_Long_Long_Integer (Right)
                 in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
          Post => Long_Long_Long_Integer (Span_Nanoseconds ("*"'Result))
                  = Long_Long_Long_Integer (Span_Nanoseconds (Left)) * Long_Long_Long_Integer (Right);
   function "*" (Left : Integer; Right : Time_Span) return Time_Span
     with Global => null,
          Pre => Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Span_Nanoseconds (Right))
                 in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
          Post => Long_Long_Long_Integer (Span_Nanoseconds ("*"'Result))
                  = Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Span_Nanoseconds (Right));
   --  (truncated toward zero, as Integer division is)
   function "/" (Left, Right : Time_Span) return Integer
     with Global => null,
          Pre => Span_Nanoseconds (Right) /= 0
                 and then not (Span_Nanoseconds (Left) = Long_Long_Integer'First and then Span_Nanoseconds (Right) = -1),
          Post => Long_Long_Integer ("/"'Result) = Span_Nanoseconds (Left) / Span_Nanoseconds (Right);
   function "/" (Left : Time_Span; Right : Integer) return Time_Span
     with Global => null,
          Pre => Right /= 0 and then not (Span_Nanoseconds (Left) = Long_Long_Integer'First and then Right = -1),
          Post => Span_Nanoseconds ("/"'Result) = Span_Nanoseconds (Left) / Long_Long_Integer (Right);

   function "<" (Left, Right : Time_Span) return Boolean
     with Global => null,
          Post => "<"'Result = (Span_Nanoseconds (Left) < Span_Nanoseconds (Right));
   function "<=" (Left, Right : Time_Span) return Boolean
     with Global => null,
          Post => "<="'Result = (Span_Nanoseconds (Left) <= Span_Nanoseconds (Right));
   function ">" (Left, Right : Time_Span) return Boolean
     with Global => null,
          Post => ">"'Result = (Span_Nanoseconds (Left) > Span_Nanoseconds (Right));
   function ">=" (Left, Right : Time_Span) return Boolean
     with Global => null,
          Post => ">="'Result = (Span_Nanoseconds (Left) >= Span_Nanoseconds (Right));

   --  a span as a Duration (which holds about 136 years either way), to the nanosecond as
   --  near as Duration's small allows, and a Duration as a span
   function To_Duration (TS : Time_Span) return Duration
     with Global => null,
          Pre => Span_Nanoseconds (TS) / 1_000_000_000 in -4_294_967_295 .. 4_294_967_295;
   function To_Time_Span (D : Duration) return Time_Span
     with Global => null,
          Post => abs (Span_Nanoseconds (To_Time_Span'Result) - Long_Long_Integer (D) * 1_000_000_000) <= 500_000_000;

   function Nanoseconds (NS : Integer) return Time_Span
     with Global => null,
          Post => Span_Nanoseconds (Nanoseconds'Result) = Long_Long_Integer (NS);
   --  (a span is a Long_Long_Integer of nanoseconds: about 292 years either way)
   function Microseconds (US : Integer) return Time_Span
     with Global => null,
          Pre => US in -9_223_372_036_854_775 .. 9_223_372_036_854_775,
          Post => Span_Nanoseconds (Microseconds'Result) = Long_Long_Integer (US) * 1_000;
   function Milliseconds (MS : Integer) return Time_Span
     with Global => null,
          Pre => MS in -9_223_372_036_854 .. 9_223_372_036_854,
          Post => Span_Nanoseconds (Milliseconds'Result) = Long_Long_Integer (MS) * 1_000_000;
   function Seconds (S : Integer) return Time_Span
     with Global => null,
          Pre => S in -9_223_372_036 .. 9_223_372_036,
          Post => Span_Nanoseconds (Seconds'Result) = Long_Long_Integer (S) * 1_000_000_000;
   function Minutes (M : Integer) return Time_Span
     with Global => null,
          Pre => M in -153_722_867 .. 153_722_867,
          Post => Span_Nanoseconds (Minutes'Result) = Long_Long_Integer (M) * 60_000_000_000;

   --  whole seconds (RM D.8(29)): those of any Time
   type Seconds_Count is range -9_223_372_037 .. 9_223_372_037;

   --  a time as whole seconds and the nanoseconds after them, 0 .. 999_999_999
   procedure Split (T : Time; SC : out Seconds_Count; TS : out Time_Span)
     with Global => null,
          Post => Span_Nanoseconds (TS) in 0 .. 999_999_999
                  and then Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS))
                           = Long_Long_Long_Integer (Nanoseconds_Of (T));

   --  the time Split gives these of
   function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time
     with Global => null,
          Pre => Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS))
                 in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
          Post => Long_Long_Long_Integer (Nanoseconds_Of (Time_Of'Result))
                  = Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS));
private
   type Time is new Long_Long_Integer;
   type Time_Span is new Long_Long_Integer;

   Time_First : constant Time := Time (Long_Long_Integer'First);
   Time_Last : constant Time := Time (Long_Long_Integer'Last);
   Time_Span_First : constant Time_Span := Time_Span (Long_Long_Integer'First);
   Time_Span_Last : constant Time_Span := Time_Span (Long_Long_Integer'Last);
   Time_Span_Zero : constant Time_Span := 0;
   Time_Span_Unit : constant Time_Span := 1;
   Tick : constant Time_Span := 1;

   function Nanoseconds_Of (T : Time) return Long_Long_Integer is (Long_Long_Integer (T));
   function Span_Nanoseconds (TS : Time_Span) return Long_Long_Integer is (Long_Long_Integer (TS));

   function "+" (Left : Time; Right : Time_Span) return Time is
     (if Long_Long_Integer (Right) >= 0
      then (if Long_Long_Integer (Left) <= Long_Long_Integer'Last - Long_Long_Integer (Right)
            then Time (Long_Long_Integer (Left) + Long_Long_Integer (Right)) else Time_Last)
      else (if Long_Long_Integer (Left) >= Long_Long_Integer'First - Long_Long_Integer (Right)
            then Time (Long_Long_Integer (Left) + Long_Long_Integer (Right)) else Time_First));
   function "+" (Left : Time_Span; Right : Time) return Time is (Right + Left);
   function "-" (Left : Time; Right : Time_Span) return Time is
     (if Long_Long_Integer (Right) <= 0
      then (if Long_Long_Integer (Left) <= Long_Long_Integer'Last + Long_Long_Integer (Right)
            then Time (Long_Long_Integer (Left) - Long_Long_Integer (Right)) else Time_Last)
      else (if Long_Long_Integer (Left) >= Long_Long_Integer'First + Long_Long_Integer (Right)
            then Time (Long_Long_Integer (Left) - Long_Long_Integer (Right)) else Time_First));
   function "-" (Left : Time; Right : Time) return Time_Span is (Time_Span (Long_Long_Integer (Left) - Long_Long_Integer (Right)));
   function "<" (Left, Right : Time) return Boolean is (Long_Long_Integer (Left) < Long_Long_Integer (Right));
   function "<=" (Left, Right : Time) return Boolean is (Long_Long_Integer (Left) <= Long_Long_Integer (Right));
   function ">" (Left, Right : Time) return Boolean is (Long_Long_Integer (Left) > Long_Long_Integer (Right));
   function ">=" (Left, Right : Time) return Boolean is (Long_Long_Integer (Left) >= Long_Long_Integer (Right));

   function "+" (Left, Right : Time_Span) return Time_Span is (Time_Span (Long_Long_Integer (Left) + Long_Long_Integer (Right)));
   function "-" (Left, Right : Time_Span) return Time_Span is (Time_Span (Long_Long_Integer (Left) - Long_Long_Integer (Right)));
   function "-" (Right : Time_Span) return Time_Span is (Time_Span (-Long_Long_Integer (Right)));
   function "abs" (Right : Time_Span) return Time_Span is (Time_Span (abs Long_Long_Integer (Right)));
   function "*" (Left : Time_Span; Right : Integer) return Time_Span is
     (Time_Span (Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Right)));
   function "*" (Left : Integer; Right : Time_Span) return Time_Span is
     (Time_Span (Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Right)));
   function "/" (Left, Right : Time_Span) return Integer is (Integer (Long_Long_Integer (Left) / Long_Long_Integer (Right)));
   function "/" (Left : Time_Span; Right : Integer) return Time_Span is (Time_Span (Long_Long_Integer (Left) / Long_Long_Integer (Right)));
   function "<" (Left, Right : Time_Span) return Boolean is (Long_Long_Integer (Left) < Long_Long_Integer (Right));
   function "<=" (Left, Right : Time_Span) return Boolean is (Long_Long_Integer (Left) <= Long_Long_Integer (Right));
   function ">" (Left, Right : Time_Span) return Boolean is (Long_Long_Integer (Left) > Long_Long_Integer (Right));
   function ">=" (Left, Right : Time_Span) return Boolean is (Long_Long_Integer (Left) >= Long_Long_Integer (Right));

   function Nanoseconds (NS : Integer) return Time_Span is (Time_Span (NS));
   function Microseconds (US : Integer) return Time_Span is (Time_Span (Long_Long_Integer (US) * 1_000));
   function Milliseconds (MS : Integer) return Time_Span is (Time_Span (Long_Long_Integer (MS) * 1_000_000));
   function Seconds (S : Integer) return Time_Span is (Time_Span (Long_Long_Integer (S) * 1_000_000_000));
   function Minutes (M : Integer) return Time_Span is (Time_Span (Long_Long_Integer (M) * 60_000_000_000));

   function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time is
     (Time (Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (TS)));
end Ada.Real_Time;
