library colibrilib.stda — module ExportThe functions without precondition, for the extraction: a division by zero raises Division_by_zero, the square root of a negative number Negative, and to_z of a number that is not an integer NotInteger
module Export656use colibrilib.spec.Value657use int.Int658use real.RealInfix659use real.FromInt660use real.Square661use real.Truncate662use colibrilib.zarith.Q as ZQ663use colibrilib.stdq.StdQ'impl as SQ664use colibrilib.stdq.StdQ as IQ665use colibrilib.stdq.Export as SQE666use mach.int.Int63667use StdA'impl668669use real.PowerInt as RP670671exception Negative672exception NotInteger673Valid (70ms) (colibri2)674let inv (a:t) : t675ensures { value1 real (fun x -> 1. /. x) a result }676raises { SQE.DivZero -> real a = 0. }677= if is_zero a then raise SQE.DivZero;678div (of_int 1) a679Valid (65ms) (colibri2)680let div (a b:t) : t681ensures { value2 real (/.) a b result }682raises { SQE.DivZero -> real b = 0. }683= if is_zero b then raise SQE.DivZero; div a b684Valid (61ms) (colibri2)685let div_e (a b:t) : t686ensures { value2 real (fun x y -> from_int (Real.div_e x y)) a b result }687raises { SQE.DivZero -> real b = 0. }688= if is_zero b then raise SQE.DivZero; div_e a b689Valid (60ms) (colibri2)690let div_t (a b:t) : t691ensures { value2 real (fun x y -> from_int (Real.div_t x y)) a b result }692raises { SQE.DivZero -> real b = 0. }693= if is_zero b then raise SQE.DivZero; div_t a b694Valid (58ms) (colibri2)695let div_f (a b:t) : t696ensures { value2 real (fun x y -> from_int (Real.div_f x y)) a b result }697raises { SQE.DivZero -> real b = 0. }698= if is_zero b then raise SQE.DivZero; div_f a b699Valid (54ms) (colibri2)700let mod_e (a b:t) : t701ensures { value2 real Real.mod_e a b result }702raises { SQE.DivZero -> real b = 0. }703= if is_zero b then raise SQE.DivZero; mod_e a b704Valid (55ms) (colibri2)705let mod_t (a b:t) : t706ensures { value2 real Real.mod_t a b result }707raises { SQE.DivZero -> real b = 0. }708= if is_zero b then raise SQE.DivZero; mod_t a b709Valid (58ms) (colibri2)710let mod_f (a b:t) : t711ensures { value2 real Real.mod_f a b result }712raises { SQE.DivZero -> real b = 0. }713= if is_zero b then raise SQE.DivZero; mod_f a b714Valid (64ms) (colibri2)715let sqrt (a:t) : t716ensures { value1 real Square.sqrt a result }717raises { Negative -> real a <. 0. }718= if sign a < 0 then raise Negative; StdA'impl.sqrt a719
a^n for n >= 0; Negative for a negative exponent
Valid (65ms) (colibri2)let pow (a:t) (n:int63) : t722ensures { value1 real (fun x -> RP.power x n) a result }723raises { Negative -> n < 0 }724= if n < 0 then raise Negative; pow a n725726exception PowDomain727
a^e, the non negative root; PowDomain outside of its domain
Valid (1.5s) (colibri2)let positive_pow [@spec:none "a root: the e.den-th power of the result is the e.num-th power of a"] (a:t) (e:ZQ.t) : t730ensures { 0. <=. real result }731ensures { 0 <= e.ZQ.num ->732RP.power (real result) e.ZQ.den = RP.power (real a) e.ZQ.num }733ensures { e.ZQ.num < 0 ->734RP.power (real result) e.ZQ.den *. RP.power (real a) (- e.ZQ.num) = 1. }735raises { PowDomain -> not (0. <. real a \/ (real a = 0. /\ 0 < e.ZQ.num)) }736= let s = sign a in737if s < 0 || (s = 0 && ZQ.sign e <= 0) then raise PowDomain;738positive_pow a (IQ.of_zarith e)739
The value of an integer
Valid (56ms) (colibri2)let to_z (a:t) : int742ensures { value0 from_int (real a) result }743raises { NotInteger -> not (exists z. real a = from_int z) }744= if not (is_integer a) then raise NotInteger; to_z a745
floor (a / m) * m: for m > 0, the largest multiple of m below a
Valid (71ms) (colibri2)let round_down_to (a:t) (m:ZQ.t) : ZQ.t749ensures { value0 ZQ.real (from_int (Truncate.floor (real a /. ZQ.real m)) *. ZQ.real m) result }750raises { SQE.DivZero -> ZQ.real m = 0. }751= match a with752| Q q -> SQE.round_down_to (IQ.to_zarith q) m753| A ia ->754if SQ.is_zero m then raise SQE.DivZero;755let q' = CA.floor (CA.div ia.ca (CA.of_q m)) in756SQ.mul (SQ.of_bigint q') m757end758
ceil (a / m) * m
Valid (71ms) (colibri2)let round_up_to (a:t) (m:ZQ.t) : ZQ.t761ensures { value0 ZQ.real (from_int (Truncate.ceil (real a /. ZQ.real m)) *. ZQ.real m) result }762raises { SQE.DivZero -> ZQ.real m = 0. }763= match a with764| Q q -> SQE.round_up_to (IQ.to_zarith q) m765| A ia ->766if SQ.is_zero m then raise SQE.DivZero;767let q' = CA.ceil (CA.div ia.ca (CA.of_q m)) in768SQ.mul (SQ.of_bigint q') m769end770771use real.Abs as RA772use colibrilib.spec.Test773Valid (30ms) (colibri2)774let compare_int (a b:t) : int63775ensures { result > 0 <-> real a >. real b }776ensures { result < 0 <-> real a <. real b }777= StdA'impl.compare_int a b778Valid (37ms) (colibri2)779let equal (a b:t) : bool780ensures { decides (real a = real b) result }781= StdA'impl.equal a b782Valid (32ms) (colibri2)783let sign (a:t) : int63784ensures { result > 0 <-> real a >. 0. }785ensures { result < 0 <-> real a <. 0. }786= StdA'impl.sign a787Valid (34ms) (colibri2)788let is_integer (a:t) : bool789ensures { decides (exists z. real a = from_int z) result }790= StdA'impl.is_integer a791Valid (28ms) (colibri2)792let of_q (q:IQ.t) : t793ensures { value0 real (IQ.real q) result }794= StdA'impl.of_q q795Valid (27ms) (colibri2)796let of_bigint (z:int) : t797ensures { value0 real (from_int z) result }798= StdA'impl.of_bigint z799Valid (36ms) (colibri2)800let of_int (z:int63) : t801ensures { value0 real (from_int z) result }802= StdA'impl.of_int z803Valid (39ms) (colibri2)804let floor (a:t) : t805ensures { value1 real (fun v -> from_int (Truncate.floor v)) a result }806= StdA'impl.floor a807Valid (35ms) (colibri2)808let ceil (a:t) : t809ensures { value1 real (fun v -> from_int (Truncate.ceil v)) a result }810= StdA'impl.ceil a811Valid (26ms) (colibri2)812let floor_z (a:t) : int813ensures { value_is (Truncate.floor (real a)) result }814= StdA'impl.floor_z a815Valid (29ms) (colibri2)816let ceil_z (a:t) : int817ensures { value_is (Truncate.ceil (real a)) result }818= StdA'impl.ceil_z a819Valid (31ms) (colibri2)820let truncate_z (a:t) : int821ensures { value_is (Truncate.truncate (real a)) result }822= StdA'impl.truncate_z a823Valid (22ms) (colibri2)824let truncate (a:t) : t825ensures { value1 real (fun v -> from_int (Truncate.truncate v)) a result }826= StdA'impl.truncate a827Valid (28ms) (colibri2)828let neg (a:t) : t829ensures { value1 real (fun x -> -. x) a result }830= StdA'impl.neg a831Valid (28ms) (colibri2)832let abs (a:t) : t833ensures { value1 real RA.abs a result }834= StdA'impl.abs a835Valid (33ms) (colibri2)836let add (a b:t) : t837ensures { value2 real (+.) a b result }838= StdA'impl.add a b839Valid (33ms) (colibri2)840let sub (a b:t) : t841ensures { value2 real (-.) a b result }842= StdA'impl.sub a b843Valid (32ms) (colibri2)844let mul (a b:t) : t845ensures { value2 real (*.) a b result }846= StdA'impl.mul a b847Valid (35ms) (colibri2)848let is_zero (a:t) : bool849ensures { decides (real a = 0.) result }850= StdA'impl.is_zero a851Valid (24ms) (colibri2)852let view (a:t) : view853ensures { match result with854| Rat q -> real a = IQ.real q855| Irr -> forall q:IQ.t. real a <> IQ.real q856end }857= StdA'impl.view a858
Raised when a precision is not positive
exception BadPrec861Valid (45ms) (colibri2)862let floor_q (prec:int) (a:t) : IQ.t863ensures { 0 < prec /\ IQ.real result <=. real a <. IQ.real result +. 1. /. from_int prec }864raises { BadPrec -> prec <= 0 }865= if prec <= 0 then raise BadPrec else StdA'impl.floor_q prec a866Valid (39ms) (colibri2)867let ceil_q (prec:int) (a:t) : IQ.t868ensures { 0 < prec /\ IQ.real result -. 1. /. from_int prec <. real a <=. IQ.real result }869raises { BadPrec -> prec <= 0 }870= if prec <= 0 then raise BadPrec else StdA'impl.ceil_q prec a871Valid (42ms) (colibri2)872let round_down_to_small (bigq: IQ.t -> bool) (max_size_bit prec:int) (x:t) : t873ensures { 0 < prec }874ensures { match result with Q _ -> true | A _ -> false end }875ensures { real result <=. real x }876raises { BadPrec -> prec <= 0 }877= if prec <= 0 then raise BadPrec else StdA'impl.round_down_to_small bigq max_size_bit prec x878Valid (39ms) (colibri2)879let round_up_to_small (bigq: IQ.t -> bool) (max_size_bit prec:int) (x:t) : t880ensures { 0 < prec }881ensures { match result with Q _ -> true | A _ -> false end }882ensures { real x <=. real result }883raises { BadPrec -> prec <= 0 }884= if prec <= 0 then raise BadPrec else StdA'impl.round_up_to_small bigq max_size_bit prec x885end886