--  Verada.Lemmas.Floating_Point: what rounding to nearest preserves, for Float and
--  Long_Float: order under rounded operations, and exactness on small integers. Each is proved, not assumed: the
--  error model (Verada.Proof.ErrorModel) knows rounding is monotone and exact on integers in
--  the significand's range, so a caller rarely needs these; they state the fact by name where
--  a proof wants it as a step.
package Verada.Lemmas.Floating_Point is

   --  addition and subtraction keep order
   procedure Lemma_Add_Is_Monotonic (Val1, Val2, Val3 : Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and abs Val1 <= Float'Last / 2.0 and abs Val2 <= Float'Last / 2.0
                 and abs Val3 <= Float'Last / 2.0,
          Post => Val1 + Val3 <= Val2 + Val3;

   procedure Lemma_Sub_Is_Monotonic (Val1, Val2, Val3 : Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and abs Val1 <= Float'Last / 2.0 and abs Val2 <= Float'Last / 2.0
                 and abs Val3 <= Float'Last / 2.0,
          Post => Val1 - Val3 <= Val2 - Val3;

   --  multiplication by a value of known sign keeps or reverses order
   procedure Lemma_Mul_Is_Monotonic (Val1, Val2, Val3 : Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 >= 0.0 and abs Val1 <= 1.0E15 and abs Val2 <= 1.0E15
                 and Val3 <= 1.0E15,
          Post => Val1 * Val3 <= Val2 * Val3;

   procedure Lemma_Mul_Is_Antimonotonic (Val1, Val2, Val3 : Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 <= 0.0 and abs Val1 <= 1.0E15 and abs Val2 <= 1.0E15
                 and Val3 >= -1.0E15,
          Post => Val1 * Val3 >= Val2 * Val3;

   --  a whole number within the significand's range converts exactly
   procedure Lemma_Integer_To_Float_Exact (N : Integer)
     with Ghost, Global => null,
          Pre => N in -2 ** 24 .. 2 ** 24,
          Post => Integer (Float (N)) = N;

   procedure Lemma_Add_Is_Monotonic (Val1, Val2, Val3 : Long_Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and abs Val1 <= Long_Float'Last / 2.0 and abs Val2 <= Long_Float'Last / 2.0
                 and abs Val3 <= Long_Float'Last / 2.0,
          Post => Val1 + Val3 <= Val2 + Val3;

   procedure Lemma_Sub_Is_Monotonic (Val1, Val2, Val3 : Long_Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and abs Val1 <= Long_Float'Last / 2.0 and abs Val2 <= Long_Float'Last / 2.0
                 and abs Val3 <= Long_Float'Last / 2.0,
          Post => Val1 - Val3 <= Val2 - Val3;

   procedure Lemma_Mul_Is_Monotonic (Val1, Val2, Val3 : Long_Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 >= 0.0 and abs Val1 <= 1.0E150 and abs Val2 <= 1.0E150
                 and Val3 <= 1.0E150,
          Post => Val1 * Val3 <= Val2 * Val3;

   procedure Lemma_Mul_Is_Antimonotonic (Val1, Val2, Val3 : Long_Float)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 <= 0.0 and abs Val1 <= 1.0E150 and abs Val2 <= 1.0E150
                 and Val3 >= -1.0E150,
          Post => Val1 * Val3 >= Val2 * Val3;

   procedure Lemma_Integer_To_Float_Exact (N : Long_Long_Integer)
     with Ghost, Global => null,
          Pre => N in -2 ** 53 .. 2 ** 53,
          Post => Long_Long_Integer (Long_Float (N)) = N;
end Verada.Lemmas.Floating_Point;
