library colibrilib.fbin — module FpBaseFp: Impl refines fp.IntfFpNames gives the names of Intf that Impl lacks. Fp is Impl seen through Intf: the clone proves every axiom of Intf and the specification of every val on the concrete floats, and the users of Fp (fp_facts.mlw, fp_dyadic.mlw, fp_interval.mlw, fp_conv.mlw) see only Intf. The extracted code calls the functions of Impl directly, with the formats of Format.
module FpBase4183use int.Int4184use int.Power4185use int.EuclideanDivision as ED4186use real.RealInfix4187use real.FromInt4188use option.Option4189use colibrilib.fp.Widen4190use colibrilib.fp.Ereal4191use colibrilib.dyadic.Dyadic as D4192use Pow2r4193use Format4194use Float4195use Round4196use real.Truncate4197use Impl4198use Integral4199use Exact4200use Nearest as N4201Valid (48ms) (colibri2)4202let of_int (fm: format) (m: mode) (z: bool) (i: int) : t4203ensures { result = round_f fm m z (Fin (from_int i)) }4204= {proof…} assert { from_int i /. from_int 1 = from_int i }; {qed}4205round_q fm m z i 14206Valid (54ms) (colibri2)4207let nb_sign_v (fm: format) : int4208ensures { result = nb_sign fm }4209= nb_sign_p fm4210Valid (54ms) (colibri2)4211let binade_v (fm: format) : int4212ensures { result = binade fm }4213= binade_p fm4214Valid (49ms) (colibri2)4215let of_index_v (fm: format) (i: int) : t4216ensures { result = of_index fm i }4217= of_index_p fm i4218Valid (45ms) (colibri2)4219let to_dyadic (x: t) : (int, int)4220ensures { let (n, e) = result in is_finite x -> to_real x = from_int n *. pow2r e }4221= dyadic_of x4222Valid (46ms) (colibri2)4223let format_of (x: t) : format4224ensures { result = x.fmt }4225= format_of x4226Valid (50ms) (colibri2)4227let eq_format (a b: format) : bool4228ensures { result <-> a = b }4229= eq_format a b4230Valid (43ms) (colibri2)4231let succ_diff_zero (x: t) : t4232ensures { not is_nan x /\ not is_plus_infinity x -> result = succ_dz x }4233= succ_diff_zero x4234Valid (44ms) (colibri2)4235let pred_diff_zero (x: t) : t4236ensures { not is_nan x /\ not is_minus_infinity x -> result = pred_dz x }4237= pred_diff_zero x4238Valid (51ms) (colibri2)4239let mid_index (x y: t) : t4240ensures { not is_nan x /\ not is_nan y /\ x.fmt = y.fmt ->4241result = of_index x.fmt (ED.div (index x + index y) 2) }4242= mid_index x y4243Valid (51ms) (colibri2)4244let widen (fm: format) : option format4245ensures { match result with4246| None -> false4247| Some fm' -> fm'.eb = fm.eb /\ fm'.sb = fm.sb + 1 end }4248= widen fm4249Valid (62ms) (colibri2)4250let le_diff_zero (x y: t) : bool4251ensures { result <-> le_dz x y }4252= le_diff_zero x y4253Valid (49ms) (colibri2)4254let lt_diff_zero (x y: t) : bool4255ensures { result <-> lt_dz x y }4256= lt_diff_zero x y4257Valid (44ms) (colibri2)4258let ghost index_range (x: t) : unit4259requires { not is_nan x }4260ensures { - nb_sign x.fmt <= index x < nb_sign x.fmt }4261= index_range x4262Valid (54ms) (colibri2)4263let ghost of_index_range (fm: format) (i: int) : unit4264requires { - nb_sign fm <= i < nb_sign fm }4265ensures { not is_nan (of_index fm i) /\ index (of_index fm i) = i }4266ensures { (of_index fm i).fmt = fm }4267= of_index_range fm i4268Valid (57ms) (colibri2)4269let add (m: mode) (x y: t) : t4270ensures { x.fmt = y.fmt -> result = fp_add m x y }4271= add m x y4272Valid (60ms) (colibri2)4273let mul (m: mode) (x y: t) : t4274ensures { x.fmt = y.fmt -> result = fp_mul m x y }4275= mul m x y4276Valid (43ms) (colibri2)4277let convert (fm: format) (m: mode) (x: t) : t4278ensures { result = fp_convert fm m x }4279= convert fm m x4280Valid (48ms) (colibri2)4281let div (m: mode) (x y: t) : t4282ensures { x.fmt = y.fmt -> result = fp_div m x y }4283= Impl.div m x y4284Valid (57ms) (colibri2)4285let sqrt (m: mode) (x: t) : t4286ensures { result = fp_sqrt m x }4287= sqrt m x4288Valid (57ms) (colibri2)4289let round_to_integral (m: mode) (x: t) : t4290ensures { result = fp_rti m x }4291= round_to_integral m x4292Valid (55ms) (colibri2)4293let ghost int_exact (fm: format) (i: int) : t4294requires { - power 2 fm.sb <= i <= power 2 fm.sb }4295requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. from_int i <=.4296to_real (of_index fm (nb_sign fm - 2)) }4297ensures { result.fmt = fm /\ not is_nan result /\ ext result = Fin (from_int i) }4298= int_exact fm i4299Valid (60ms) (colibri2)4300let ghost widen_exact (fm': format) (x: t) : t4301requires { x.fmt.eb <= fm'.eb /\ x.fmt.sb <= fm'.sb }4302requires { not is_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }4303ensures { result.fmt = fm' /\ not is_nan result /\ ext result = ext x }4304= widen_exact fm' x4305Valid (55ms) (colibri2)4306let ghost float_dyadic (x: t) : unit4307requires { not is_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }4308ensures { D.fix (to_real x) (qmin x.fmt) /\ D.flt (to_real x) x.fmt.sb }4309= float_dyadic x4310Valid (54ms) (colibri2)4311let ghost round_fix (fm: format) (m: mode) (z: bool) (r: real) (e: int) : unit4312requires { D.fix r e /\ e <= qmin fm + power 2 fm.eb - 3 }4313requires { not is_plus_infinity (round_f fm m z (Fin r)) }4314requires { not is_minus_infinity (round_f fm m z (Fin r)) }4315ensures { D.fix (to_real (round_f fm m z (Fin r))) e }4316= round_fix fm m z r e4317Valid (61ms) (colibri2)4318let ghost round_zero_small (fm: format) (m: mode) (z: bool) (r: real) : unit4319requires { r <> 0.0 /\ is_zero (round_f fm m z (Fin r)) }4320ensures { -. pow2r (qmin fm) <. r <. pow2r (qmin fm) }4321= round_zero_small fm m z r4322Valid (52ms) (colibri2)4323let ghost dyadic_exact (fm: format) (r: real) : t4324requires { D.fix r (qmin fm) /\ D.flt r fm.sb }4325requires { -. to_real (of_index fm (nb_sign fm - 2)) <=. r <=.4326to_real (of_index fm (nb_sign fm - 2)) }4327ensures { result.fmt = fm /\ not is_nan result /\ ext result = Fin r }4328= dyadic_exact fm r4329Valid (60ms) (colibri2)4330let ghost max_float_ge (fm: format) : unit4331ensures { pow2r (qmin fm + power 2 fm.eb - 3 + fm.sb - 1) <=.4332to_real (of_index fm (nb_sign fm - 2)) }4333= max_float_ge fm4334Valid (55ms) (colibri2)4335let ghost near_double (m: mode) (z: bool) (x: t) (r: real) : unit4336requires { m = RNE \/ m = RNA }4337requires { not is_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }4338ensures { 2.0 *. to_real x <=. r ->4339not is_plus_infinity (round_f x.fmt m z (Fin r)) ->43402.0 *. to_real x <=. to_real (of_index x.fmt (nb_sign x.fmt - 2)) }4341ensures { r <=. 2.0 *. to_real x ->4342not is_minus_infinity (round_f x.fmt m z (Fin r)) ->4343-. to_real (of_index x.fmt (nb_sign x.fmt - 2)) <=. 2.0 *. to_real x }4344= near_double m z x r4345
The facts of Intf on round_int, from its definition
Valid (412ms) (colibri2)let ghost round_int_ge (m: mode) (r: real) (n: int) : unit4348requires { n <= round_int m r }4349ensures { match m with4350| RTN -> from_int n <=. r4351| RTP -> from_int n -. 1.0 <=. r4352| RTZ -> if 0 < n then from_int n <=. r else from_int n -. 1.0 <=. r4353| RNE | RNA -> from_int n -. 0.5 <=. r4354end }4355= {proof…}4356let f = Truncate.floor r in4357assert { from_int f <=. r <. from_int f +. 1.0 };4358assert { r <. 0.0 -> f <= -1 };4359assert { 0.0 <=. r -> 0 <= f }4360{qed}4361Valid (107ms) (colibri2)4362let ghost round_int_le (m: mode) (r: real) (n: int) : unit4363requires { round_int m r <= n }4364ensures { match m with4365| RTN -> r <=. from_int n +. 1.04366| RTP -> r <=. from_int n4367| RTZ -> if n < 0 then r <=. from_int n else r <=. from_int n +. 1.04368| RNE | RNA -> r <=. from_int n +. 0.54369end }4370= {proof…}4371let f = Truncate.floor r in4372assert { from_int f <=. r <. from_int f +. 1.0 };4373assert { r <. 0.0 -> f <= -1 };4374assert { 0.0 <=. r -> 0 <= f }4375{qed}4376Valid (68ms) (colibri2)4377let ghost round_int_ceil (r: real) (k: int) : unit4378requires { r <=. from_int k }4379ensures { round_int RTP r <= k }4380= {proof…}4381let f = Truncate.floor r in4382assert { from_int f <=. r <. from_int f +. 1.0 };4383assert { f <= k };4384assert { r -. from_int f <> 0.0 -> f < k }4385{qed}4386Valid (58ms) (colibri2)4387let ghost round_int_floor (r: real) (k: int) : unit4388requires { from_int k <=. r }4389ensures { k <= round_int RTN r }4390= {proof…}4391let f = Truncate.floor r in4392assert { from_int f <=. r <. from_int f +. 1.0 };4393assert { k < f + 1 }4394{qed}4395Valid (68ms) (colibri2)4396let round_int_v (m: mode) (x: t) : int4397requires { not is_nan x /\ not is_plus_infinity x /\ not is_minus_infinity x }4398ensures { result = round_int m (to_real x) }4399= match to_integer m x with4400| Some n -> n4401| None -> absurd4402end4403Valid (51ms) (colibri2)4404let of_q (fm: format) (m: mode) (z: bool) (n d: int) : t4405requires { 0 < d }4406ensures { result = round_f fm m z (Fin (from_int n /. from_int d)) }4407= round_q fm m z n d4408
The types and the logic of Intf
type format = Format.format4411type t = Float.t4412function fmt (x: t) : format = x.Float.fmt4413function eb (fm: format) : int = fm.Format.eb4414function sb (fm: format) : int = fm.Format.sb4415let predicate is_nan (x: t) = Impl.is_nan x4416predicate is_plus_infinity (x: t) = Impl.is_plus_infinity x4417predicate is_minus_infinity (x: t) = Impl.is_minus_infinity x4418function not_a_number (fm: format) : t = Impl.not_a_number fm4419function plus_infinity (fm: format) : t = Impl.plus_infinity fm4420function minus_infinity (fm: format) : t = Impl.minus_infinity fm4421predicate le_dz (x y: t) = Impl.le_dz x y4422predicate lt_dz (x y: t) = Impl.lt_dz x y4423predicate is_zero (x: t) = Impl.is_zero x4424predicate is_positive (x: t) = Impl.is_positive x4425predicate is_subnormal (x: t) = Impl.is_subnormal x4426predicate is_normal (x: t) = Impl.is_normal x4427function index (x: t) : int = Impl.index x4428function of_index (fm: format) (i: int) : t = Impl.of_index fm i4429let function neg (x: t) : t = Impl.neg x4430let function abs (x: t) : t = Impl.abs x4431function min (x y: t) : t = Impl.min x y4432function max (x y: t) : t = Impl.max x y4433function to_real (x: t) : real = Float.to_real x4434function ext (x: t) : ereal = Impl.ext x4435function round_f (fm: format) (m: mode) (z: bool) (e: ereal) : t = Float.round_f fm m z e4436function round_int (m: mode) (r: real) : int = Impl.round_int m r4437
The index, the widths, the bounds of a real and of a sum, as data
Valid (43ms) (colibri2)let index_v (x: t) : int4440requires { not is_nan x }4441ensures { result = index x }4442= index_t x4443Valid (50ms) (colibri2)4444let eb_v (fm: format) : int4445ensures { result = eb fm }4446= fm.Format.eb4447Valid (50ms) (colibri2)4448let sb_v (fm: format) : int4449ensures { result = sb fm }4450= fm.Format.sb4451Valid (85ms) (colibri2)4452let mk_format_o (e s: int) : option format4453ensures { match result with4454| Some fm -> eb fm = e /\ sb fm = s4455| None -> not (1 < e /\ 1 < s) end }4456= if 1 < e && 1 < s then Some { Format.eb = e; Format.sb = s } else None4457
The linear bounds a r + b <= f <= c r + d of a rounding f to nearest of a real r, when f is in [lo, hi] (Nearest.nearest_bounds): `b = ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) gives a = am 2^ae / ad, b = bm 2^be`, c = cm 2^ce / cd, d = dm 2^de
predicate nearest_ok_base (lo hi: t) (b: ((int, int), int, (int, int), (int, int), int, (int, int))) =4464let ((am, ae), ad, (bm, be), (cm, ce), cd, (dm, de)) = b in44650 < ad /\ 0 < cd /\4466forall m z r. (m = RNE \/ m = RNA) ->4467le_dz lo (round_f (fmt lo) m z (Fin r)) -> le_dz (round_f (fmt lo) m z (Fin r)) hi ->4468from_int am *. pow2r ae *. r +. from_int ad *. (from_int bm *. pow2r be)4469<=. from_int ad *. to_real (round_f (fmt lo) m z (Fin r)) /\4470from_int cd *. to_real (round_f (fmt lo) m z (Fin r))4471<=. from_int cm *. pow2r ce *. r +. from_int cd *. (from_int dm *. pow2r de)4472Valid (1.5s) (colibri2)4473let nearest_bounds (lo hi: t) : option ((int, int), int, (int, int), (int, int), int, (int, int))4474ensures { match result with Some b -> nearest_ok_base lo hi b | None -> true end }4475= match N.nearest_bounds lo hi with4476| None -> None4477| Some b ->4478Some ((b.N.lo_coef.dm, b.N.lo_coef.de), b.N.lo_den,4479(b.N.lo_cst.dm, b.N.lo_cst.de),4480(b.N.hi_coef.dm, b.N.hi_coef.de), b.N.hi_den,4481(b.N.hi_cst.dm, b.N.hi_cst.de))4482end4483Valid (304ms) (colibri2) (cvc5) (split_vc) (depth 1)4484let float_lo (f: t) (strict exact: bool) : t4485ensures { fmt result = fmt f }4486ensures { forall r. f = round_f (fmt f) RTP (r <. 0.0) (Fin r) ->4487(exact <-> not is_nan f /\ not is_infinity f /\ to_real f = r) ->4488forall v. fmt v = fmt f -> not is_nan v -> not is_infinity v ->4489(if strict then r <. to_real v else r <=. to_real v) -> le_dz result v }4490= Impl.float_lo f strict exact4491Valid (1.2s) (colibri2) (cvc5) (split_vc) (depth 1)4492let float_hi (f: t) (strict exact: bool) : t4493ensures { fmt result = fmt f }4494ensures { forall r. f = round_f (fmt f) RTN (r <. 0.0) (Fin r) ->4495(exact <-> not is_nan f /\ not is_infinity f /\ to_real f = r) ->4496forall v. fmt v = fmt f -> not is_nan v -> not is_infinity v ->4497(if strict then to_real v <. r else to_real v <=. r) -> le_dz v result }4498= Impl.float_hi f strict exact4499
The sum as Intf.fp_add states it on these symbols
function fp_add_i (m: mode) (x y: t) : t =4502if is_nan x \/ is_nan y \/ opposite (ext x) (ext y) then not_a_number (fmt x)4503else round_f (fmt x) m4504(if is_positive x <-> is_positive y then (if is_positive x then false else true)4505else (if m = RTN then true else false))4506(eadd (ext x) (ext y))4507Valid (151ms) (colibri2 3) (cvc5 2) (split_vc 2) (depth 2)4508let add_bounds (lo hi: t) : option (t, t)4509ensures { match result with4510| Some (a, b) -> fmt a = fmt lo /\ fmt b = fmt lo /\4511forall m x y. fmt x = fmt lo -> fmt y = fmt lo ->4512le_dz lo (fp_add_i m x y) -> le_dz (fp_add_i m x y) hi ->4513le_dz a x /\ le_dz x b4514| None -> true4515end }4516= {proof…}4517let lemma eq (m: mode) (x y: t) : unit4518ensures { fp_add_i m x y = fp_add m x y }4519= () in4520{qed}4521AddBound.add_bounds lo hi4522Valid (55ms) (colibri2)4523let round_bracket (fm: format) (m: mode) (n1 d1 n2 d2: int) : option t4524ensures { match result with4525| None -> d1 <= 0 \/ d2 <= 0 \/4526round_f fm m (n1 < 0) (Fin (from_int n1 /. from_int d1))4527<> round_f fm m (n2 < 0) (Fin (from_int n2 /. from_int d2))4528| Some x -> forall r. from_int n1 /. from_int d1 <=. r <=. from_int n2 /. from_int d2 ->4529x = round_f fm m (r <. 0.0) (Fin r)4530end }4531= Impl.round_bracket fm m n1 d1 n2 d24532Valid (49ms) (colibri2)4533let round_between (fm: format) (m: mode) (n k: int) : t4534requires { power 2 (eb fm - 1) + sb fm - 2 <= k }4535ensures { forall r. from_int n /. from_int (power 2 k) <. r <. from_int (n + 1) /. from_int (power 2 k) ->4536result = round_f fm m (r <. 0.0) (Fin r) }4537= {proof…} assert { 1 - qmin fm = power 2 (eb fm - 1) + sb fm - 2 }; {qed}4538Impl.round_between fm m n k4539
Intf.ext_widen, stated on these symbols
Valid (49ms) (colibri2)let lemma ext_widen_fp (fm fm': format) (i: int)4542requires { eb fm' = eb fm /\ sb fm' = sb fm + 1 }4543requires { - Format.nb_sign fm <= i < Format.nb_sign fm }4544ensures { ext (of_index fm' (windex i)) = ext (of_index fm i) }4545= {proof…} Impl.ext_widen fm fm' i {qed}45464547end4548