--  Verada.Lemmas.Arithmetic: facts of integer arithmetic, the nonlinear ones above all,
--  that solvers do not find by themselves inside a larger proof: order under multiplication
--  and division, signs, squares, quotients and remainders. Each is a ghost procedure whose postcondition is proved (by
--  Verada's release gate, like the rest of the library); call one where a proof needs its fact
--  as a step. Products and quotients are stated in Long_Long_Long_Integer (128 bits), so
--  that no lemma has an overflow to rule out first.
package Verada.Lemmas.Arithmetic with Pure is
   subtype Big is Long_Long_Long_Integer;

   --  multiplication by a value of known sign keeps or reverses order
   procedure Lemma_Mult_Is_Monotonic (Val1, Val2, Val3 : Integer)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 >= 0,
          Post => Big (Val1) * Big (Val3) <= Big (Val2) * Big (Val3);

   procedure Lemma_Mult_Is_Strictly_Monotonic (Val1, Val2, Val3 : Integer)
     with Ghost, Global => null,
          Pre => Val1 < Val2 and Val3 > 0,
          Post => Big (Val1) * Big (Val3) < Big (Val2) * Big (Val3);

   procedure Lemma_Mult_Is_Antimonotonic (Val1, Val2, Val3 : Integer)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 <= 0,
          Post => Big (Val1) * Big (Val3) >= Big (Val2) * Big (Val3);

   --  a product's sign
   procedure Lemma_Mult_Non_Negative (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => (Val1 >= 0 and Val2 >= 0) or (Val1 <= 0 and Val2 <= 0),
          Post => Big (Val1) * Big (Val2) >= 0;

   procedure Lemma_Mult_Non_Positive (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => (Val1 >= 0 and Val2 <= 0) or (Val1 <= 0 and Val2 >= 0),
          Post => Big (Val1) * Big (Val2) <= 0;

   --  a product's size
   procedure Lemma_Mult_Abs (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val1 > Integer'First and Val2 > Integer'First,
          Post => abs (Big (Val1) * Big (Val2)) = Big (abs Val1) * Big (abs Val2);

   procedure Lemma_Mult_Bounded (Val1, Val2, Bound1, Bound2 : Integer)
     with Ghost, Global => null,
          Pre => Val1 in 0 .. Bound1 and Val2 in 0 .. Bound2,
          Post => Big (Val1) * Big (Val2) <= Big (Bound1) * Big (Bound2);

   --  squares
   procedure Lemma_Square_Is_Monotonic (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => 0 <= Val1 and Val1 <= Val2,
          Post => Big (Val1) * Big (Val1) <= Big (Val2) * Big (Val2);

   procedure Lemma_Square_Non_Negative (Val : Integer)
     with Ghost, Global => null,
          Post => Big (Val) * Big (Val) >= 0;

   --  division by a positive value keeps order; a larger divisor gives less
   procedure Lemma_Div_Is_Monotonic (Val1, Val2, Val3 : Integer)
     with Ghost, Global => null,
          Pre => Val1 <= Val2 and Val3 > 0,
          Post => Val1 / Val3 <= Val2 / Val3;

   procedure Lemma_Div_Is_Antimonotonic (Val1, Val2, Val3 : Integer)
     with Ghost, Global => null,
          Pre => Val1 >= 0 and 0 < Val2 and Val2 <= Val3,
          Post => Val1 / Val3 <= Val1 / Val2;

   --  a quotient and a remainder put the dividend back together
   procedure Lemma_Div_Then_Mult_Bounds (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val1 >= 0 and Val2 > 0,
          Post => Big (Val1 / Val2) * Big (Val2) <= Big (Val1)
                  and Big (Val1) - Big (Val1 / Val2) * Big (Val2) < Big (Val2)
                  and Big (Val1 / Val2) * Big (Val2) + Big (Val1 rem Val2) = Big (Val1);

   procedure Lemma_Mult_Then_Div (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val2 /= 0 and then Big (Val1) * Big (Val2) in Big (Integer'First) .. Big (Integer'Last),
          Post => (Val1 * Val2) / Val2 = Val1;

   --  a quotient is no larger than its dividend
   procedure Lemma_Div_Is_Smaller (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val1 >= 0 and Val2 > 0,
          Post => Val1 / Val2 <= Val1;

   --  remainders and modular sums
   procedure Lemma_Mod_Range (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val2 > 0,
          Post => Val1 mod Val2 in 0 .. Val2 - 1;

   procedure Lemma_Mod_Of_Smaller (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val2 > 0 and then Val1 in 0 .. Val2 - 1,
          Post => Val1 mod Val2 = Val1;

   procedure Lemma_Mod_Mult (Val1, Val2 : Integer)
     with Ghost, Global => null,
          Pre => Val2 > 0 and then Big (Val1) * Big (Val2) in Big (Integer'First) .. Big (Integer'Last),
          Post => (Val1 * Val2) mod Val2 = 0;
end Verada.Lemmas.Arithmetic;
