library colibrilib.qa — module RatFacts about the rationals, used in the proofs only. Like those of Dv, they are let ghost functions that a proof calls.
module Rat40use int.Int41use real.RealInfix42use real.FromInt43use colibrilib.stdq.StdQ44Valid (42ms) (colibri2)45let ghost rat_q (q:StdQ.t) : unit46ensures { is_rat (real q) }47= ()48Valid (43ms) (colibri2)49let ghost rat_int (k:int) : unit50ensures { is_rat (from_int k) }51= {proof…} let q = of_bigint k in52assert { real q = from_int k } {qed}53Valid (57ms) (colibri2)54let ghost rat_add (x y:real) : unit55requires { is_rat x }56requires { is_rat y }57ensures { is_rat (x +. y) }58= {proof…} let a = any StdQ.t ensures { real result = x } in59let b = any StdQ.t ensures { real result = y } in60let c = add a b in61assert { real c = x +. y } {qed}62Valid (42ms) (colibri2)63let ghost rat_sub (x y:real) : unit64requires { is_rat x }65requires { is_rat y }66ensures { is_rat (x -. y) }67= {proof…} let a = any StdQ.t ensures { real result = x } in68let b = any StdQ.t ensures { real result = y } in69let c = sub a b in70assert { real c = x -. y } {qed}71Valid (51ms) (colibri2)72let ghost rat_mul_int (k:int) (x:real) : unit73requires { is_rat x }74ensures { is_rat (from_int k *. x) }75= {proof…} let a = any StdQ.t ensures { real result = x } in76let c = mul (of_bigint k) a in77assert { real c = from_int k *. x } {qed}78
x is a rational iff c * x is, for an integer c that is not zero
Valid (61ms) (colibri2)let ghost rat_scale (c:int) (x:real) : unit81requires { c <> 0 }82ensures { is_rat (from_int c *. x) <-> is_rat x }83= {proof…} if pure { is_rat x } then rat_mul_int c x;84if pure { is_rat (from_int c *. x) } then begin85let a = any StdQ.t ensures { real result = from_int c *. x } in86assert { from_int c <> 0.0 };87let c' = of_bigint c in88let b = div a c' in89assert { real b = x }90end {qed}91Valid (42ms) (colibri2)92let ghost rat_neg (x:real) : unit93ensures { is_rat (0.0 -. x) <-> is_rat x }94= {proof…} rat_int 0;95assert { from_int 0 = 0.0 };96if pure { is_rat x } then rat_sub 0.0 x;97if pure { is_rat (0.0 -. x) } then begin98rat_sub 0.0 (0.0 -. x);99assert { 0.0 -. (0.0 -. x) = x }100end {qed}101
Adding a multiple of a rational does not change the rationality
Valid (43ms) (colibri2)let ghost rat_shift (x d:real) (k:int) : unit104requires { is_rat d }105ensures { is_rat (x +. from_int k *. d) <-> is_rat x }106= {proof…} if pure { is_rat x } then begin107rat_mul_int k d;108rat_add x (from_int k *. d)109end;110if pure { is_rat (x +. from_int k *. d) } then begin111rat_mul_int k d;112rat_sub (x +. from_int k *. d) (from_int k *. d);113assert { x +. from_int k *. d -. from_int k *. d = x }114end {qed}115
a - n where n is -a moved by a multiple of a rational
Valid (33ms) (colibri2 3) (cvc5) (split_vc) (depth 1)let ghost rat_double (a n d:real) (k:int) : unit118requires { is_rat d }119requires { n = -. a +. from_int k *. d }120ensures { is_rat (a -. n) <-> is_rat a }121= {proof…} assert { a -. n = from_int 2 *. a +. from_int (- k) *. d122by from_int (- k) = -. from_int k /\ from_int 2 = 2.0 };123rat_shift (from_int 2 *. a) d (- k);124rat_scale 2 a {qed}125end126