Common — library colibrilib.fp_interval — module FpInterval

The domain

522
module FpInterval
523
use int.Int
524
use mach.int.Int63
525
use int.Power
526
use list.List
527
use option.Option
528
use real.RealInfix
529
use real.FromInt
530
use colibrilib.fp.Ereal
531
use colibrilib.fbin.Fp
532
use colibrilib.fp_facts.Facts
533
use Intervals

NaN or a union of intervals; fp_interval.ml never returns the empty domain

  Valid (23ms) (colibri2)
537
type
dom
= {
u
: list (t, t);
nan
: bool; ghost
fm
: format }
538
invariant { ok_u u /\ fmt_u fm u }
539
by { u = Nil; nan = true; fm = any format }

The floats of the format d.fm in d

542
predicate
mem
(v: t) (d: dom) =
543
fmt v = d.fm /\ if is_nan v then d.nan else mem_u v d.u
544
545
predicate
is_bot
(d: dom) = d.u = Nil /\ not d.nan
546
Valid (322ms) (colibri2)
547
let
union
(ghost fm: format) (d1 d2: dom) : dom
548
requires { d1.fm = fm /\ d2.fm = fm }
549
ensures { result.fm = fm }
550
ensures { forall v. mem v result <-> mem v d1 \/ mem v d2 }
551
= { u = union_u fm d1.u d2.u; nan = d1.nan || d2.nan; fm = fm }
552
Valid (302ms) (cvc5)
553
let
inter
(ghost fm: format) (d1 d2: dom) : option dom
554
requires { d1.fm = fm /\ d2.fm = fm }
555
ensures { match result with Some r -> r.fm = fm | None -> true end }
556
ensures { match result with
557
| None -> forall v. not (mem v d1 /\ mem v d2)
558
| Some d -> not (is_bot d) /\ forall v. mem v d <-> mem v d1 /\ mem v d2
559
end }
560
= let u = inter_u fm d1.u d2.u in
561
let nan = d1.nan && d2.nan in
562
match u with
563
| Nil -> if nan then Some { u = u; nan = nan; fm = fm } else None
564
| _ -> Some { u = u; nan = nan; fm = fm }
565
end
566
Valid (71ms) (colibri2 12) (split_vc 8) (depth 4)
567
let
is_distinct
(ghost fm: format) (d1 d2: dom) : bool
568
requires { d1.fm = fm /\ d2.fm = fm }
569
ensures { result <-> forall v. not (mem v d1 /\ mem v d2) }
570
= let r = (not d1.nan || not d2.nan) && is_distinct_u fm d1.u d2.u in
571
{proof…}
572
assert { d1.nan /\ d2.nan -> mem (not_a_number fm) d1 /\ mem (not_a_number fm) d2 };
573
let lemma pt (v: t) : unit
574
requires { mem_u v d1.u /\ mem_u v d2.u }
575
ensures { mem v d1 /\ mem v d2 }
576
= mem_u_not_nan v d1.u in
577
{qed}
578
r
579
Valid (340ms) (cvc5)
580
let
diff
(ghost fm: format) (d1 d2: dom) : option dom
581
requires { d1.fm = fm /\ d2.fm = fm }
582
ensures { match result with Some r -> r.fm = fm | None -> true end }
583
ensures { match result with
584
| None -> forall v. not (mem v d1 /\ not mem v d2)
585
| Some d -> not (is_bot d) /\ forall v. mem v d <-> mem v d1 /\ not mem v d2
586
end }
587
= let l = diff_u fm d1.u d2.u in
588
let nan = d1.nan && not d2.nan in
589
{proof…}
590
let lemma pt (v: t) : unit
591
requires { mem_u v d1.u \/ mem_u v l }
592
ensures { not is_nan v }
593
= if pure { mem_u v d1.u } then mem_u_not_nan v d1.u
594
else mem_u_not_nan v l in
595
{qed}
596
match l with
597
| Nil -> if nan then Some { u = l; nan = nan; fm = fm } else None
598
| _ -> Some { u = l; nan = nan; fm = fm }
599
end
600
Valid (57ms) (colibri2 11) (split_vc 6) (depth 4)
601
let
is_included
(ghost fm: format) (d1 d2: dom) : bool
602
requires { d1.fm = fm /\ d2.fm = fm }
603
ensures { result <-> forall v. mem v d1 -> mem v d2 }
604
= let r = (not d1.nan || d2.nan) && is_included_u fm d1.u d2.u in
605
{proof…}
606
assert { d1.nan /\ not d2.nan -> mem (not_a_number fm) d1 /\ not mem (not_a_number fm) d2 };
607
let lemma pt (v: t) : unit
608
requires { mem_u v d1.u }
609
ensures { mem v d1 /\ (mem v d2 <-> mem_u v d2.u) }
610
= mem_u_not_nan v d1.u in
611
{qed}
612
r

Exact: Included when included (also for the empty domain, which fp_interval.ml never passes), else Disjoint when disjoint, else Uncomparable

  Valid (517ms) (colibri2 13) (cvc5 2) (split_vc 3) (depth 2)
617
let
is_distinct_or_included
(ghost fm: format) (d1 d2: dom) : comparison
618
requires { d1.fm = fm /\ d2.fm = fm }
619
ensures { result = Included <-> forall v. mem v d1 -> mem v d2 }
620
ensures { result = Disjoint <->
621
(forall v. not (mem v d1 /\ mem v d2)) /\
622
not (forall v. mem v d1 -> mem v d2) }
623
= {proof…}
624
let lemma pt (v: t) : unit
625
requires { mem_u v d1.u }
626
ensures { mem v d1 /\ (mem v d2 <-> mem_u v d2.u) }
627
= mem_u_not_nan v d1.u in
628
assert { d1.nan -> mem (not_a_number fm) d1 /\ (mem (not_a_number fm) d2 <-> d2.nan) };
629
assert { forall v. is_nan v -> fmt v = fm ->
630
(mem v d1 <-> d1.nan) /\ (mem v d2 <-> d2.nan) };
631
{qed}
632
if d1.nan then
633
if d2.nan then (if is_included_u fm d1.u d2.u then Included else Uncomparable)
634
else (if is_distinct_u fm d1.u d2.u then Disjoint else Uncomparable)
635
else match d1.u with
636
| Nil -> Included
637
| Cons _ _ -> {proof…} head_mem d1.u; {qed} is_distinct_or_included_u fm d1.u d2.u
638
end
639
640
type
singleton
=
NaN
|
No
|
Sin
t

Exact: NaN when the domain is the NaNs, Sin a when it is {a}, No otherwise. F.equal a b of the OCaml is le_diff_zero b a here (a <= b is known).

  Valid (321ms) (colibri2 18) (cvc5) (split_vc 4) (depth 2)
645
let
is_singleton
(ghost fm: format) (d: dom) : singleton
646
requires { d.fm = fm }
647
ensures { match result with
648
| NaN -> forall v. mem v d <-> fmt v = fm /\ is_nan v
649
| Sin a -> not (is_nan a) /\ forall v. mem v d <-> v = a
650
| No -> not (forall v. mem v d <-> fmt v = fm /\ is_nan v) /\
651
forall a. not (forall v. mem v d <-> v = a)
652
end }
653
= {proof…}
654
assert { forall v. is_nan v -> fmt v = fm -> (mem v d <-> d.nan) };
655
let lemma pt (v: t) : unit
656
requires { mem_u v d.u }
657
ensures { mem v d }
658
= mem_u_not_nan v d.u in
659
{qed}
660
match d.u with
661
| Nil ->
662
if d.nan then NaN
663
else begin
664
{proof…} assert { not mem (not_a_number fm) d };
665
assert { forall v. not mem v d };
666
let lemma no_sin (a: t) : unit
667
ensures { not (forall v. mem v d <-> v = a) }
668
= assert { not mem a d } in {qed}
669
No
670
end
671
| Cons (a, b) r ->
672
{proof…} head_mem d.u; le_not_nan_any a b; le_refl_any b;
673
assert { mem a d /\ mem b d }; {qed}
674
if d.nan then begin
675
{proof…} assert { mem (not_a_number fm) d }; {qed}
676
No
677
end else match r with
678
| Nil ->
679
if le_diff_zero b a then begin
680
{proof…}
681
let lemma pt2 (v: t) : unit
682
requires { in_iv v a b } ensures { v = a }
683
= le_not_nan_any a v; le_trans_any v b a;
684
assert { index v = index a } in
685
{qed}
686
Sin a
687
end else begin
688
{proof…} total b a; assert { a <> b by index a < index b }; {qed}
689
No
690
end
691
| Cons (a', _) _ ->
692
{proof…} head_mem r; assert { mem a' d };
693
lt_le_trans_any b a' a'; le_lt_trans_any a b a';
694
assert { a <> a' by index a < index a' }; {qed}
695
No
696
end
697
end
698
699
exception
Empty

The least float that is not NaN

  Valid (20ms) (colibri2)
702
let
min_f
(ghost fm: format) (d: dom) : t
703
requires { d.fm = fm }
704
ensures { fmt result = fm }
705
raises { Empty -> d.u = Nil }
706
ensures { mem_u result d.u /\ forall v. mem_u v d.u -> le_dz result v }
707
= match d.u with
708
| Nil -> raise Empty
709
| Cons (a, b) r ->
710
{proof…} head_mem d.u;
711
(match r with Nil -> () | Cons _ _ -> sorted_above b r end);
712
let lemma pt (v: t) : unit
713
requires { mem_u v d.u } ensures { le_dz a v }
714
= if pure { mem_u v r } then begin
715
le_lt_trans_any a b v; lt_le_any a v
716
end in
717
{qed}
718
a
719
end

The last interval of a list

  Valid (58ms) (colibri2)
722
let rec
last_iv
(ghost fm: format) (l: list (t, t)) : (t, t)
723
requires { fmt_u fm l }
724
requires { ok_u l /\ l <> Nil }
725
ensures { let (a, b) = result in in_iv b a b /\ mem_u b l }
726
ensures { let (_, b) = result in forall v. mem_u v l -> le_dz v b }
727
variant { l }
728
= match l with
729
| Nil -> absurd
730
| Cons (a, b) Nil -> {proof…} le_not_nan_any a b; le_refl_any b; {qed} (a, b)
731
| Cons (_, b) r ->
732
let res = last_iv fm r in
733
{proof…} sorted_above b r;
734
let (_, b') = res in
735
let lemma pt (v: t) : unit
736
requires { mem_u v l } ensures { le_dz v b' }
737
= if pure { mem_u v r } then () else begin
738
lt_le_any b b'; le_trans_any v b b' end in
739
{qed}
740
res
741
end

The greatest float that is not NaN

  Valid (132ms) (colibri2)
744
let
max_f
(ghost fm: format) (d: dom) : t
745
requires { d.fm = fm }
746
ensures { fmt result = fm }
747
raises { Empty -> d.u = Nil }
748
ensures { mem_u result d.u /\ forall v. mem_u v d.u -> le_dz v result }
749
= match d.u with
750
| Nil -> raise Empty
751
| Cons _ _ -> let (_, b) = last_iv fm d.u in b
752
end

All the floats; minf and pinf are the infinities of the format

  Valid (46ms) (colibri2 16) (split_vc 2) (depth 2)
755
let
top
(ghost fm: format) (minf pinf: t) : dom
756
requires { fmt minf = fm /\ fmt pinf = fm }
757
ensures { result.fm = fm }
758
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
759
ensures { forall v. fmt v = fm -> mem v result }
760
= {proof…}
761
minus_infinity_lt pinf;
762
let lemma pt (v: t) : unit
763
requires { is_not_nan v /\ fmt v = fm } ensures { in_iv v minf pinf }
764
= if pure { is_plus_infinity v } then le_refl v
765
else begin lt_plus_infinity v; lt_le v pinf end;
766
if pure { is_minus_infinity v } then le_refl v
767
else begin minus_infinity_lt v; lt_le minf v end in
768
lt_le minf pinf;
769
{qed}
770
{ u = Cons (minf, pinf) Nil; nan = true; fm = fm }
771
Valid (292ms) (colibri2)
772
let
complement
(ghost fm: format) (minf pinf: t) (d: dom) : option dom
773
requires { fmt minf = fm /\ fmt pinf = fm /\ d.fm = fm }
774
ensures { match result with Some r -> r.fm = fm | None -> true end }
775
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
776
ensures { match result with
777
| None -> forall v. fmt v = fm -> mem v d
778
| Some c -> not (is_bot c) /\ forall v. mem v c <-> fmt v = fm /\ not mem v d
779
end }
780
= diff fm (top fm minf pinf) d

All the floats but f (but the NaNs when f is NaN)

  Valid (1.5s) (colibri2 37) (cvc4 5) (cvc5 4) (split_vc) (depth 1)
783
let
except
(ghost fm: format) (minf pinf: t) (f: t) : dom
784
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt f = fm }
785
ensures { result.fm = fm }
786
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
787
ensures { forall v. mem v result <->
788
fmt v = fm /\ if is_nan f then not (is_nan v) else v <> f }
789
= {proof…}
790
let ghost all = top fm minf pinf in
791
minus_infinity_lt pinf; lt_le minf pinf;
792
{qed}
793
if is_nan f then { u = Cons (minf, pinf) Nil; nan = false; fm = fm }
794
else if le_diff_zero f minf then begin
795
{proof…}
796
assert { f = minf by index f <= index minf
797
so (is_minus_infinity f \/ lt_dz minf f) };
798
succ_gt f; succ_least f pinf;
799
let lemma pt (v: t) : unit
800
requires { is_not_nan v /\ fmt v = fm } ensures { v <> f <-> in_iv v (succ_dz f) pinf }
801
= assert { mem v all };
802
if pure { v <> f } then begin
803
assert { lt_dz f v by index f < index v
804
so index f <= index v /\ index f <> index v };
805
succ_least f v end
806
else lt_le_trans f (succ_dz f) pinf;
807
if pure { in_iv v (succ_dz f) pinf } then lt_le_trans f (succ_dz f) v in
808
{qed}
809
{ u = Cons (succ_diff_zero f, pinf) Nil; nan = true; fm = fm }
810
end else if le_diff_zero pinf f then begin
811
{proof…}
812
total minf f;
813
assert { f = pinf by index pinf <= index f
814
so (is_plus_infinity f \/ lt_dz f pinf) };
815
pred_lt f; pred_greatest f minf;
816
let lemma pt (v: t) : unit
817
requires { is_not_nan v /\ fmt v = fm } ensures { v <> f <-> in_iv v minf (pred_dz f) }
818
= assert { mem v all };
819
if pure { v <> f } then begin
820
assert { lt_dz v f by index v < index f
821
so index v <= index f /\ index f <> index v };
822
pred_greatest f v end;
823
if pure { in_iv v minf (pred_dz f) } then le_lt_trans v (pred_dz f) f in
824
{qed}
825
{ u = Cons (minf, pred_diff_zero f) Nil; nan = true; fm = fm }
826
end else begin
827
{proof…}
828
total minf f; total f pinf;
829
succ_gt f; succ_least f pinf; pred_lt f; pred_greatest f minf;
830
lt_le_trans (pred_dz f) f (succ_dz f);
831
let lemma pt (v: t) : unit
832
requires { is_not_nan v /\ fmt v = fm }
833
ensures { v <> f <-> in_iv v minf (pred_dz f) \/ in_iv v (succ_dz f) pinf }
834
= assert { mem v all };
835
total_any v f;
836
if pure { lt_dz v f } then pred_greatest f v;
837
if pure { lt_dz f v } then succ_least f v;
838
if pure { in_iv v minf (pred_dz f) } then le_lt_trans v (pred_dz f) f;
839
if pure { in_iv v (succ_dz f) pinf } then lt_le_trans f (succ_dz f) v in
840
{qed}
841
{ u = Cons (minf, pred_diff_zero f) (Cons (succ_diff_zero f, pinf) Nil);
842
nan = true; fm = fm }
843
end

The first float of a list is its least

  Valid (17ms) (colibri2)
846
let ghost
head_min
(fm: format) (l: list (t, t)) : unit
847
requires { fmt_u fm l }
848
requires { ok_u l }
849
ensures { match l with Nil -> true
850
| Cons (a, _) _ -> forall v. mem_u v l -> le_dz a v end }
851
= {proof…} match l with
852
| Nil -> ()
853
| Cons (a, b) r ->
854
(match r with Nil -> () | Cons _ _ -> sorted_above b r end);
855
let lemma pt (v: t) : unit
856
requires { mem_u v l } ensures { le_dz a v }
857
= if pure { mem_u v r } then begin
858
le_lt_trans_any a b v; lt_le_any a v
859
end in
860
()
861
end {qed}
862
863
predicate
is_inf
(v: t) = is_plus_infinity v \/ is_minus_infinity v

No NaN and no infinity

  Valid (595ms) (colibri2 11) (cvc4) (split_vc 4) (depth 4)
866
let
is_finite
(ghost fm: format) (minf pinf: t) (d: dom) : bool
867
requires { fmt minf = fm /\ fmt pinf = fm /\ d.fm = fm }
868
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
869
ensures { result <-> forall v. mem v d -> not (is_nan v) /\ not (is_inf v) }
870
= {proof…}
871
assert { d.nan -> mem (not_a_number fm) d };
872
let lemma pt (v: t) : unit
873
requires { mem_u v d.u } ensures { mem v d }
874
= mem_u_not_nan v d.u in
875
{qed}
876
if d.nan then false
877
else match d.u with
878
| Nil -> true
879
| Cons (a, _) _ ->
880
let (_, b) = last_iv fm d.u in
881
{proof…} head_min fm d.u; {qed}
882
not (le_diff_zero a minf) && not (le_diff_zero pinf b)
883
end

An infinity is in the domain

  Valid (743ms) (cvc4)
886
let
has_infinites
(ghost fm: format) (minf pinf: t) (d: dom) : bool
887
requires { fmt minf = fm /\ fmt pinf = fm /\ d.fm = fm }
888
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
889
ensures { result <-> exists v. mem v d /\ is_inf v }
890
= match d.u with
891
| Nil -> false
892
| Cons (a, _) _ ->
893
let (_, b) = last_iv fm d.u in
894
{proof…} head_min fm d.u; head_mem d.u;
895
let lemma pt (v: t) : unit
896
requires { mem_u v d.u } ensures { mem v d }
897
= mem_u_not_nan v d.u in
898
{qed}
899
le_diff_zero a minf || le_diff_zero pinf b
900
end

The classes of floats

The bounds are the special values of the format (Farith's get_special_values), given with their index.

907
predicate
at_index
(fm: format) (x: t) (i: int) =
908
fmt x = fm /\ is_not_nan x /\ index x = i

Facts on the format used for the bounds

  Valid (30ms) (colibri2 7) (split_vc) (depth 1)
911
let ghost
format_facts
(fm: format) () : unit
912
ensures { 2 <= power 2 ((sb fm) - 1) }
913
ensures { 3 * power 2 ((sb fm) - 1) + 1 <= (nb_sign fm) }
914
= {proof…}
915
assert { power 2 ((sb fm) - 1) = 2 * power 2 ((sb fm) - 2) };
916
assert { power 2 (eb fm) = 2 * power 2 ((eb fm) - 1) /\ power 2 ((eb fm) - 1) >= 2 };
917
assert { 2 <= power 2 ((sb fm) - 1) };
918
assert { power 2 (eb fm) - 1 >= 3 };
919
assert { (power 2 (eb fm) - 1) * power 2 ((sb fm) - 1) >= 3 * power 2 ((sb fm) - 1) }
920
{qed}

Two intervals, one of each sign

  Valid (30ms) (colibri2)
923
let
two
(ghost fm: format) (a b c d: t) : dom
924
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
925
ensures { result.fm = fm }
926
requires { is_not_nan a /\ is_not_nan b /\ is_not_nan c /\ is_not_nan d }
927
requires { index a <= index b < index c <= index d }
928
ensures { forall v. mem v result <->
929
fmt v = fm /\ is_not_nan v /\ (index a <= index v <= index b \/ index c <= index v <= index d) }
930
= { u = Cons (a, b) (Cons (c, d) Nil); nan = false; fm = fm }

One interval

  Valid (17ms) (colibri2)
933
let
one
(ghost fm: format) (a b: t) : dom
934
requires { fmt a = fm /\ fmt b = fm }
935
ensures { result.fm = fm }
936
requires { is_not_nan a /\ is_not_nan b /\ index a <= index b }
937
ensures { forall v. mem v result <->
938
fmt v = fm /\ is_not_nan v /\ index a <= index v <= index b }
939
= { u = Cons (a, b) Nil; nan = false; fm = fm }
940
Valid (549ms) (cvc4)
941
let
normal
(ghost fm: format) (nl nf pf pl: t) : dom
942
requires { fmt nl = fm /\ fmt nf = fm /\ fmt pf = fm /\ fmt pl = fm }
943
ensures { result.fm = fm }
944
requires { at_index fm nl (1 - (nb_sign fm)) /\ at_index fm nf (-1 - power 2 ((sb fm) - 1)) }
945
requires { at_index fm pf (power 2 ((sb fm) - 1)) /\ at_index fm pl ((nb_sign fm) - 2) }
946
ensures { forall v. mem v result <-> fmt v = fm /\ is_normal v }
947
= {proof…} format_facts fm (); {qed}
948
two fm nl nf pf pl
949
Valid (561ms) (cvc4)
950
let
subnormal
(ghost fm: format) (nl nf pf pl: t) : dom
951
requires { fmt nl = fm /\ fmt nf = fm /\ fmt pf = fm /\ fmt pl = fm }
952
ensures { result.fm = fm }
953
requires { at_index fm nl (- power 2 ((sb fm) - 1)) /\ at_index fm nf (-2) }
954
requires { at_index fm pf 1 /\ at_index fm pl (power 2 ((sb fm) - 1) - 1) }
955
ensures { forall v. mem v result <-> fmt v = fm /\ is_subnormal v }
956
= {proof…} format_facts fm (); {qed}
957
two fm nl nf pf pl
958
Valid (25ms) (colibri2)
959
let
zeros
(ghost fm: format) (nz pz: t) : dom
960
requires { fmt nz = fm /\ fmt pz = fm }
961
ensures { result.fm = fm }
962
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
963
ensures { forall v. mem v result <-> fmt v = fm /\ is_zero v }
964
= one fm nz pz
965
Valid (533ms) (cvc5)
966
let
infinites
(ghost fm: format) (minf pinf: t) : dom
967
requires { fmt minf = fm /\ fmt pinf = fm }
968
ensures { result.fm = fm }
969
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
970
ensures { forall v. mem v result <-> fmt v = fm /\ is_inf v }
971
= {proof…} format_facts fm (); {qed}
972
two fm minf minf pinf pinf

Neither NaN nor infinite

  Valid (54ms) (colibri2 8) (split_vc 2) (depth 2)
975
let
finites
(ghost fm: format) (nl pl: t) : dom
976
requires { fmt nl = fm /\ fmt pl = fm }
977
ensures { result.fm = fm }
978
requires { at_index fm nl (1 - (nb_sign fm)) /\ at_index fm pl ((nb_sign fm) - 2) }
979
ensures { forall v. mem v result <-> fmt v = fm /\ is_not_nan v /\ not (is_inf v) }
980
= {proof…} format_facts fm ();
981
let lemma pt (v: t) : unit
982
requires { fmt v = fm /\ is_not_nan v }
983
ensures { is_inf v <-> index v = - nb_sign fm \/ index v = nb_sign fm - 1 }
984
ensures { - nb_sign fm <= index v < nb_sign fm }
985
= index_range v in
986
{qed}
987
one fm nl pl
988
Valid (664ms) (cvc4)
989
let
negatives
(ghost fm: format) (minf nz: t) : dom
990
requires { fmt minf = fm /\ fmt nz = fm }
991
ensures { result.fm = fm }
992
requires { minf = (minus_infinity fm) /\ at_index fm nz (-1) }
993
ensures { forall v. mem v result <-> fmt v = fm /\ is_not_nan v /\ not (is_positive v) }
994
= {proof…} format_facts fm (); {qed}
995
one fm minf nz
996
Valid (499ms) (cvc5)
997
let
positives
(ghost fm: format) (pz pinf: t) : dom
998
requires { fmt pz = fm /\ fmt pinf = fm }
999
ensures { result.fm = fm }
1000
requires { at_index fm pz 0 /\ pinf = (plus_infinity fm) }
1001
ensures { forall v. mem v result <-> fmt v = fm /\ is_not_nan v /\ is_positive v }
1002
= {proof…} format_facts fm (); {qed}
1003
one fm pz pinf

The special values of a format (Farith's get_special_values)

1006
type
specials
= {
1007
minf
: t;
nl
: t;
nf
: t;
nsl
: t;
nsf
: t;
nz
: t;
1008
pz
: t;
psf
: t;
psl
: t;
pf
: t;
pl
: t;
pinf
: t;
1009
}
1010
1011
predicate
ok_specials
(fm: format) (sv: specials) =
1012
sv.minf = (minus_infinity fm) /\ sv.pinf = (plus_infinity fm) /\
1013
at_index fm sv.nl (1 - (nb_sign fm)) /\ at_index fm sv.nf (-1 - power 2 ((sb fm) - 1)) /\
1014
at_index fm sv.nsl (- power 2 ((sb fm) - 1)) /\ at_index fm sv.nsf (-2) /\
1015
at_index fm sv.nz (-1) /\ at_index fm sv.pz 0 /\
1016
at_index fm sv.psf 1 /\ at_index fm sv.psl (power 2 ((sb fm) - 1) - 1) /\
1017
at_index fm sv.pf (power 2 ((sb fm) - 1)) /\ at_index fm sv.pl ((nb_sign fm) - 2)
1018
1019
type
classify
= {
1020
clnan
: bool;
neg_infinity
: bool;
neg_normal
: bool;
neg_subnormal
: bool;
1021
clzeros
: bool;
pos_subnormal
: bool;
pos_normal
: bool;
pos_infinity
: bool;
1022
}

A float of [a, b] is in d

  Valid (373ms) (colibri2 5) (split_vc 2) (depth 2)
1025
let
meets
(ghost fm: format) (a b: t) (d: dom) : bool
1026
requires { fmt a = fm /\ fmt b = fm /\ d.fm = fm }
1027
requires { is_not_nan a /\ is_not_nan b /\ index a <= index b }
1028
ensures { result <-> exists v. mem v d /\ is_not_nan v /\ index a <= index v <= index b }
1029
= {proof…}
1030
let lemma pt (v: t) : unit
1031
requires { mem_u v d.u } ensures { mem v d /\ is_not_nan v }
1032
= mem_u_not_nan v d.u in
1033
{qed}
1034
not (is_distinct_u fm (Cons (a, b) Nil) d.u)

Exact: each flag says whether d has a float of that class

  Valid (839ms) (colibri2 13) (cvc4 10) (cvc5 5) (split_vc 5) (depth 2)
1037
let
classify_dom
(ghost fm: format) (sv: specials) (d: dom) : classify
1038
requires { d.fm = fm }
1039
requires { ok_specials fm sv }
1040
ensures { result.clnan <-> d.nan }
1041
ensures { result.neg_infinity <-> mem (minus_infinity fm) d }
1042
ensures { result.neg_normal <->
1043
exists v. mem v d /\ is_normal v /\ not is_positive v }
1044
ensures { result.neg_subnormal <->
1045
exists v. mem v d /\ is_subnormal v /\ not is_positive v }
1046
ensures { result.clzeros <-> exists v. mem v d /\ is_zero v }
1047
ensures { result.pos_subnormal <->
1048
exists v. mem v d /\ is_subnormal v /\ is_positive v }
1049
ensures { result.pos_normal <->
1050
exists v. mem v d /\ is_normal v /\ is_positive v }
1051
ensures { result.pos_infinity <-> mem (plus_infinity fm) d }
1052
= {proof…} format_facts fm ();
1053
assert { forall v. mem v d -> is_not_nan v -> (v = (minus_infinity fm) <-> index v = - (nb_sign fm)) };
1054
assert { forall v. mem v d -> is_not_nan v -> (v = (plus_infinity fm) <-> index v = (nb_sign fm) - 1) };
1055
{qed}
1056
{ clnan = d.nan;
1057
neg_infinity = meets fm sv.minf sv.minf d;
1058
neg_normal = meets fm sv.nl sv.nf d;
1059
neg_subnormal = meets fm sv.nsl sv.nsf d;
1060
clzeros = meets fm sv.nz sv.pz d;
1061
pos_subnormal = meets fm sv.psf sv.psl d;
1062
pos_normal = meets fm sv.pf sv.pl d;
1063
pos_infinity = meets fm sv.pinf sv.pinf d }

Negation and absolute value

[neg b, neg a] is the image of [a, b]

  Valid (58ms) (cvc4)
1068
let ghost
in_iv_neg
(fm: format) (v a b: t) : unit
1069
requires { is_not_nan v /\ is_not_nan a /\ is_not_nan b }
1070
ensures { in_iv v (neg b) (neg a) <-> in_iv (neg v) a b }
1071
= {proof…} () {qed}

The negation of l, reversed, in front of acc

  Valid (597ms) (colibri2 10) (cvc5 3) (split_vc 2) (depth 2)
1074
let rec
neg_rev
(ghost fm: format) (l acc: list (t, t)) : list (t, t)
1075
requires { fmt_u fm l /\ fmt_u fm acc }
1076
ensures { fmt_u fm result }
1077
requires { ok_u l /\ ok_u acc }
1078
requires { match l, acc with
1079
| Cons (a, _) _, Cons (a', _) _ -> lt_dz (neg a) a'
1080
| _ -> true end }
1081
ensures { ok_u result }
1082
ensures { forall v. mem_u v result <->
1083
(is_not_nan v /\ mem_u (neg v) l) \/ mem_u v acc }
1084
variant { l }
1085
= match l with
1086
| Nil -> acc
1087
| Cons (a, b) r ->
1088
{proof…}
1089
let lemma pt (v: t) : unit
1090
requires { is_not_nan v }
1091
ensures { in_iv v (neg b) (neg a) <-> in_iv (neg v) a b }
1092
= in_iv_neg fm v a b in
1093
let lemma nn (v: t) : unit
1094
requires { mem_u (neg v) l } ensures { is_not_nan v }
1095
= mem_u_not_nan (neg v) l in
1096
{qed}
1097
neg_rev fm r (Cons (neg b, neg a) acc)
1098
end

Exact

  Valid (99ms) (cvc4)
1101
let
neg_dom
(ghost fm: format) (d: dom) : dom
1102
requires { d.fm = fm }
1103
ensures { result.fm = fm }
1104
ensures { forall v. mem v result <-> mem (neg v) d }
1105
= {proof…}
1106
let lemma pt (v: t) : unit
1107
requires { mem_u (neg v) d.u } ensures { is_not_nan v }
1108
= mem_u_not_nan (neg v) d.u in
1109
{qed}
1110
{ u = neg_rev fm d.u Nil; nan = d.nan; fm = fm }

The absolute values of [a, b]; pz is +0

  Valid (690ms) (colibri2 7) (cvc4 4) (cvc5) (split_vc 7) (depth 4)
1113
let
abs_iv
(ghost fm: format) (pz a b: t) : list (t, t)
1114
requires { fmt pz = fm /\ fmt a = fm /\ fmt b = fm }
1115
ensures { fmt_u fm result }
1116
requires { at_index fm pz 0 /\ is_not_nan a /\ is_not_nan b /\ le_dz a b }
1117
ensures { ok_u result }
1118
ensures { forall v. mem_u v result <->
1119
is_not_nan v /\ is_positive v /\ (in_iv v a b \/ in_iv (neg v) a b) }
1120
= if le_diff_zero pz a then Cons (a, b) Nil
1121
else if lt_diff_zero b pz then Cons (abs b, abs a) Nil
1122
else union_u fm (Cons (pz, abs a) Nil) (Cons (pz, b) Nil)
1123
Valid (326ms) (cvc5)
1124
let rec
abs_u
(ghost fm: format) (pz: t) (l: list (t, t)) : list (t, t)
1125
requires { fmt pz = fm /\ fmt_u fm l }
1126
ensures { fmt_u fm result }
1127
requires { at_index fm pz 0 /\ ok_u l }
1128
ensures { ok_u result }
1129
ensures { forall v. mem_u v result <->
1130
is_not_nan v /\ is_positive v /\ (mem_u v l \/ mem_u (neg v) l) }
1131
variant { l }
1132
= match l with
1133
| Nil -> Nil
1134
| Cons (a, b) r -> union_u fm (abs_iv fm pz a b) (abs_u fm pz r)
1135
end

Exact: the positive floats whose negation or itself is in d

  Valid (190ms) (cvc4)
1138
let
abs_dom
(ghost fm: format) (pz: t) (d: dom) : dom
1139
requires { fmt pz = fm /\ d.fm = fm }
1140
ensures { result.fm = fm }
1141
requires { at_index fm pz 0 }
1142
ensures { forall v. mem v result <->
1143
fmt v = fm /\ if is_nan v then d.nan else is_positive v /\ (mem v d \/ mem (neg v) d) }
1144
ensures { forall v. mem v d -> mem (abs v) result }
1145
= let r = { u = abs_u fm pz d.u; nan = d.nan; fm = fm } in
1146
{proof…}
1147
let lemma pt (v: t) : unit
1148
requires { mem v d } ensures { mem (abs v) r }
1149
= if is_nan v then () else begin
1150
index_range v; mem_u_not_nan v d.u;
1151
if pure { is_positive v } then assert { abs v = v by
1152
of_index fm (index (abs v)) = of_index fm (index v) }
1153
else begin
1154
neg_neg v;
1155
assert { abs v = neg v by
1156
of_index fm (index (abs v)) = of_index fm (index (neg v)) }
1157
end
1158
end in
1159
{qed}
1160
r

Minimum and maximum

The result is one of the arguments: the union, exact when the arguments range over the two domains independently.

  Valid (63ms) (colibri2)
1167
let
min_dom
(ghost fm: format) (d1 d2: dom) : dom
1168
requires { d1.fm = fm /\ d2.fm = fm }
1169
ensures { result.fm = fm }
1170
ensures { forall x y. mem x d1 -> mem y d2 -> mem (min x y) result }
1171
ensures { forall v. mem v result <-> mem v d1 \/ mem v d2 }
1172
= union fm d1 d2
1173
Valid (64ms) (colibri2)
1174
let
max_dom
(ghost fm: format) (d1 d2: dom) : dom
1175
requires { d1.fm = fm /\ d2.fm = fm }
1176
ensures { result.fm = fm }
1177
ensures { forall x y. mem x d1 -> mem y d2 -> mem (max x y) result }
1178
ensures { forall v. mem v result <-> mem v d1 \/ mem v d2 }
1179
= union fm d1 d2
1180
Valid (21ms) (colibri2)
1181
let function
nan_dom
(ghost fm: format) : dom = { u = Nil; nan = true; fm = fm }

The interval [a, b] of two floats that are not NaN, a <= b

  Valid (25ms) (colibri2)
1184
let
interval
(ghost fm: format) (a b: t) : dom
1185
requires { fmt a = fm /\ fmt b = fm }
1186
ensures { result.fm = fm }
1187
requires { is_not_nan a /\ is_not_nan b /\ le_dz a b }
1188
ensures { forall v. mem v result <-> not (is_nan v) /\ in_iv v a b }
1189
= { u = Cons (a, b) Nil; nan = false; fm = fm }

The domain of one float

  Valid (83ms) (colibri2)
1192
let
singleton
(ghost fm: format) (v: t) : dom
1193
requires { fmt v = fm }
1194
ensures { result.fm = fm }
1195
ensures { mem v result }
1196
ensures { forall w. mem w result -> (is_nan v <-> is_nan w) }
1197
ensures { forall w. mem w result -> not (is_nan v) -> le_dz v w /\ le_dz w v }
1198
= if is_nan v then (nan_dom fm)
1199
else begin
1200
{proof…} le_refl_any v; {qed}
1201
interval fm v v
1202
end

Addition

By the monotonicity of add (Facts.add_box), on each pair of intervals; NaN for the infinities of opposite signs.

The sums of the box [a1, b1] x [a2, b2]

  Valid (777ms) (colibri2 22) (cvc5 3) (split_vc) (depth 1)
1210
let
add_iv
(ghost fm: format) (minf pinf: t) (m: mode) (a1 b1 a2 b2: t) : dom
1211
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt a2 = fm /\ fmt b2 = fm }
1212
ensures { result.fm = fm }
1213
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
1214
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 }
1215
requires { is_not_nan a2 /\ is_not_nan b2 /\ le_dz a2 b2 }
1216
ensures { forall x y. in_iv x a1 b1 -> in_iv y a2 b2 ->
1217
mem (fp_add m x y) result }
1218
= let a3 = add m a1 a2 in
1219
let b3 = add m b1 b2 in
1220
{proof…}
1221
let lemma inf_le (x y: t) : unit
1222
requires { le_dz x y } ensures { is_plus_infinity x -> is_plus_infinity y }
1223
ensures { is_minus_infinity y -> is_minus_infinity x }
1224
= le_not_nan x y;
1225
if pure { is_plus_infinity x /\ not is_plus_infinity y } then begin
1226
lt_plus_infinity y; plus_infinity_unique x; lt_not_le y x end;
1227
if pure { is_minus_infinity y /\ not is_minus_infinity x } then begin
1228
minus_infinity_lt x; minus_infinity_unique y; lt_not_le y x end in
1229
{qed}
1230
match is_nan a3, is_nan b3 with
1231
| True, True -> (* -inf + +inf *) (nan_dom fm)
1232
| False, False ->
1233
{proof…}
1234
add_box m a1 b1 a2 b2;
1235
let lemma pt (x y: t) : unit
1236
requires { in_iv x a1 b1 /\ in_iv y a2 b2 }
1237
requires { is_not_nan (fp_add m x y) }
1238
ensures { in_iv (fp_add m x y) a3 b3 }
1239
= add_box m a1 x a2 y; add_box m x b1 y b2 in
1240
{qed}
1241
{ u = Cons (a3, b3) Nil;
1242
nan = (le_diff_zero pinf b1 && le_diff_zero a2 minf)
1243
|| (le_diff_zero pinf b2 && le_diff_zero a1 minf); fm = fm }
1244
| True, False ->
1245
(* b1 = +inf \/ b2 = +inf *)
1246
{proof…} le_refl_any pinf; {qed}
1247
{ u = Cons (pinf, pinf) Nil; nan = true; fm = fm }
1248
| False, True ->
1249
(* a1 = -inf \/ a2 = -inf *)
1250
{proof…} le_refl_any minf; {qed}
1251
{ u = Cons (minf, minf) Nil; nan = true; fm = fm }
1252
end

The sums of [a1, b1] and l2

  Valid (85ms) (cvc5)
1255
let rec
add_iv_u
(ghost fm: format) (minf pinf: t) (m: mode) (a1 b1: t) (l2: list (t, t)) : dom
1256
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt_u fm l2 }
1257
ensures { result.fm = fm }
1258
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
1259
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 /\ ok_u l2 }
1260
ensures { forall x y. in_iv x a1 b1 -> mem_u y l2 ->
1261
mem (fp_add m x y) result }
1262
variant { l2 }
1263
= match l2 with
1264
| Nil -> { u = Nil; nan = false; fm = fm }
1265
| Cons (a2, b2) r ->
1266
union fm (add_iv fm minf pinf m a1 b1 a2 b2) (add_iv_u fm minf pinf m a1 b1 r)
1267
end
1268
Valid (55ms) (cvc5)
1269
let rec
add_u
(ghost fm: format) (minf pinf: t) (m: mode) (l1 l2: list (t, t)) : dom
1270
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt_u fm l1 /\ fmt_u fm l2 }
1271
ensures { result.fm = fm }
1272
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
1273
requires { ok_u l1 /\ ok_u l2 }
1274
ensures { forall x y. mem_u x l1 -> mem_u y l2 -> mem (fp_add m x y) result }
1275
variant { l1 }
1276
= match l1 with
1277
| Nil -> { u = Nil; nan = false; fm = fm }
1278
| Cons (a1, b1) r ->
1279
union fm (add_iv_u fm minf pinf m a1 b1 l2) (add_u fm minf pinf m r l2)
1280
end

Sound: the sums of the floats of d1 and d2 are in the result

  Valid (91ms) (cvc5)
1283
let
add_dom
(ghost fm: format) (minf pinf: t) (m: mode) (d1 d2: dom) : dom
1284
requires { fmt minf = fm /\ fmt pinf = fm /\ d1.fm = fm /\ d2.fm = fm }
1285
ensures { result.fm = fm }
1286
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) }
1287
ensures { forall x y. mem x d1 -> mem y d2 -> mem (fp_add m x y) result }
1288
= let r = add_u fm minf pinf m d1.u d2.u in
1289
if d1.nan || d2.nan then union fm (nan_dom fm) r else r

Multiplication

By quadrants of signs: in each, the product is monotonic in the magnitudes (Facts.mul_mag), the bounds are the products of the ends of small magnitude and of the ends of large magnitude.

1297
predicate
specials4
(fm: format) (minf pinf nz pz: t) =
1298
minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) /\
1299
at_index fm nz (-1) /\ at_index fm pz 0
1300
Valid (50ms) (colibri2)
1301
let
is_zero_f
(ghost fm: format) (nz pz: t) (x: t) : bool
1302
requires { fmt nz = fm /\ fmt pz = fm /\ fmt x = fm }
1303
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ is_not_nan x }
1304
ensures { result <-> is_zero x }
1305
= le_diff_zero nz x && le_diff_zero x pz
1306
Valid (61ms) (colibri2)
1307
let
is_inf_f
(ghost fm: format) (minf pinf: t) (x: t) : bool
1308
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt x = fm }
1309
requires { minf = (minus_infinity fm) /\ pinf = (plus_infinity fm) /\ is_not_nan x }
1310
ensures { result <-> is_infinity x }
1311
= {proof…}
1312
index_range x; index_range pinf; index_range minf;
1313
{qed}
1314
le_diff_zero pinf x || le_diff_zero x minf

The products of x of sign px with |s1| <= |x| <= |l1| and y of sign py with |s2| <= |y| <= |l2|

  Valid (813ms) (colibri2 37) (cvc4 5) (cvc5 12) (split_vc) (depth 1)
1318
let
mul_quad
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
1319
(px: bool) (s1 l1: t) (py: bool) (s2 l2: t) : dom
1320
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt s1 = fm /\ fmt l1 = fm /\ fmt s2 = fm /\ fmt l2 = fm }
1321
ensures { result.fm = fm }
1322
requires { specials4 fm minf pinf nz pz }
1323
requires { mag_le px s1 l1 /\ mag_le py s2 l2 }
1324
ensures { forall x y. mag_le px s1 x -> mag_le px x l1 ->
1325
mag_le py s2 y -> mag_le py y l2 -> mem (fp_mul m x y) result }
1326
= let p = mul m s1 s2 in
1327
let q = mul m l1 l2 in
1328
let pos = if px then py else not py in
1329
{proof…}
1330
mag_le_sign px s1 l1; mag_le_sign py s2 l2;
1331
let lemma pt_sign (x y: t) : unit
1332
requires { mag_le px s1 x /\ mag_le px x l1 }
1333
requires { mag_le py s2 y /\ mag_le py y l2 }
1334
ensures { mul_zneg x y <-> not pos }
1335
ensures { is_zero x -> is_zero s1 }
1336
ensures { is_zero y -> is_zero s2 }
1337
ensures { is_infinity x -> is_infinity l1 }
1338
ensures { is_infinity y -> is_infinity l2 }
1339
ensures { is_zero l1 -> is_zero x }
1340
ensures { is_zero l2 -> is_zero y }
1341
ensures { is_infinity s1 -> is_infinity x }
1342
ensures { is_infinity s2 -> is_infinity y }
1343
= mag_le_sign px s1 x; mag_le_sign px x l1;
1344
mag_le_sign py s2 y; mag_le_sign py y l2;
1345
index_range x; index_range y; index_range s1; index_range s2;
1346
index_range l1; index_range l2 in
1347
{qed}
1348
match is_nan p, is_nan q with
1349
| True, True -> (* 0 * inf at both ends *) (nan_dom fm)
1350
| True, False ->
1351
(* an infinite end and a zero one: the infinity or NaN *)
1352
let i = if pos then pinf else minf in
1353
{proof…}
1354
le_refl_any i;
1355
let lemma pt (x y: t) : unit
1356
requires { mag_le px s1 x /\ mag_le px x l1 }
1357
requires { mag_le py s2 y /\ mag_le py y l2 }
1358
ensures { is_nan (fp_mul m x y) \/ fp_mul m x y = i }
1359
= pt_sign x y; mul_special m x y; mul_special m s1 s2;
1360
mul_special m l1 l2 in
1361
{qed}
1362
{ u = Cons (i, i) Nil; nan = true; fm = fm }
1363
| False, True ->
1364
(* a zero end and an infinite one: the zero or NaN *)
1365
let z = if pos then pz else nz in
1366
{proof…}
1367
le_refl_any z;
1368
let lemma pt (x y: t) : unit
1369
requires { mag_le px s1 x /\ mag_le px x l1 }
1370
requires { mag_le py s2 y /\ mag_le py y l2 }
1371
ensures { is_nan (fp_mul m x y) \/ fp_mul m x y = z }
1372
= pt_sign x y; mul_special m x y; mul_special m s1 s2;
1373
mul_special m l1 l2;
1374
let r = fp_mul m x y in
1375
if is_not_nan r then begin
1376
index_range r;
1377
assert { r = z by of_index fm (index r) = of_index fm (index z) }
1378
end in
1379
{qed}
1380
{ u = Cons (z, z) Nil; nan = true; fm = fm }
1381
| False, False ->
1382
{proof…}
1383
mul_mag m px py s1 l1 s2 l2;
1384
let lemma pt (x y: t) : unit
1385
requires { mag_le px s1 x /\ mag_le px x l1 }
1386
requires { mag_le py s2 y /\ mag_le py y l2 }
1387
requires { is_not_nan (fp_mul m x y) }
1388
ensures { if pos then in_iv (fp_mul m x y) p q
1389
else in_iv (fp_mul m x y) q p }
1390
= mul_mag m px py s1 x s2 y; mul_mag m px py x l1 y l2 in
1391
let lemma pn (x y: t) : unit
1392
requires { mag_le px s1 x /\ mag_le px x l1 }
1393
requires { mag_le py s2 y /\ mag_le py y l2 }
1394
requires { is_nan (fp_mul m x y) }
1395
ensures { (is_zero s1 /\ is_infinity l2) \/ (is_zero s2 /\ is_infinity l1) }
1396
= pt_sign x y; mul_special m x y in
1397
{qed}
1398
{ u = Cons (if pos then (p, q) else (q, p)) Nil;
1399
nan = (is_zero_f fm nz pz s1 && is_inf_f fm minf pinf l2)
1400
|| (is_zero_f fm nz pz s2 && is_inf_f fm minf pinf l1); fm = fm }
1401
end
1402
Valid (28ms) (colibri2)
1403
let function
empty_dom
(ghost fm: format) : dom = { u = Nil; nan = false; fm = fm }

The products of the box [a1, b1] x [a2, b2], by the four quadrants

  Valid (563ms) (colibri2 50) (cvc4) (cvc5 5) (split_vc 13) (depth 4)
1406
let
mul_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a1 b1 a2 b2: t) : dom
1407
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt a2 = fm /\ fmt b2 = fm }
1408
ensures { result.fm = fm }
1409
requires { specials4 fm minf pinf nz pz }
1410
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 }
1411
requires { is_not_nan a2 /\ is_not_nan b2 /\ le_dz a2 b2 }
1412
ensures { forall x y. in_iv x a1 b1 -> in_iv y a2 b2 ->
1413
mem (fp_mul m x y) result }
1414
= {proof…}
1415
let lemma sign_split (x: t) : unit
1416
requires { is_not_nan x /\ fmt x = fm }
1417
ensures { is_positive x <-> le_dz pz x }
1418
ensures { not is_positive x <-> le_dz x nz }
1419
= index_range x in
1420
{qed}
1421
(* the negative part [a, min b -0], the positive part [max a +0, b] *)
1422
let n1 = le_diff_zero a1 nz in
1423
let p1 = le_diff_zero pz b1 in
1424
let n2 = le_diff_zero a2 nz in
1425
let p2 = le_diff_zero pz b2 in
1426
let sn1 = if le_diff_zero b1 nz then b1 else nz in
1427
let sp1 = if le_diff_zero pz a1 then a1 else pz in
1428
let sn2 = if le_diff_zero b2 nz then b2 else nz in
1429
let sp2 = if le_diff_zero pz a2 then a2 else pz in
1430
{proof…}
1431
let lemma pt (x: t) (a b sn sp: t) : unit
1432
requires { is_not_nan a /\ is_not_nan b /\ in_iv x a b /\ fmt a = fm }
1433
requires { sn = (if le_dz b nz then b else nz) }
1434
requires { sp = (if le_dz pz a then a else pz) }
1435
ensures { is_positive x -> le_dz pz b /\ mag_le true sp x /\ mag_le true x b }
1436
ensures { not is_positive x ->
1437
le_dz a nz /\ mag_le false sn x /\ mag_le false x a }
1438
= index_range x; index_range a; index_range b in
1439
let lemma ends (a b sn sp: t) : unit
1440
requires { is_not_nan a /\ is_not_nan b /\ le_dz a b /\ fmt a = fm }
1441
requires { sn = (if le_dz b nz then b else nz) }
1442
requires { sp = (if le_dz pz a then a else pz) }
1443
ensures { le_dz a nz -> mag_le false sn a }
1444
ensures { le_dz pz b -> mag_le true sp b }
1445
= index_range a; index_range b in
1446
ends a1 b1 sn1 sp1; ends a2 b2 sn2 sp2;
1447
{qed}
1448
let pp = if p1 && p2 then mul_quad fm minf pinf nz pz m true sp1 b1 true sp2 b2
1449
else (empty_dom fm) in
1450
let pn = if p1 && n2 then mul_quad fm minf pinf nz pz m true sp1 b1 false sn2 a2
1451
else (empty_dom fm) in
1452
let np = if n1 && p2 then mul_quad fm minf pinf nz pz m false sn1 a1 true sp2 b2
1453
else (empty_dom fm) in
1454
let nn = if n1 && n2 then mul_quad fm minf pinf nz pz m false sn1 a1 false sn2 a2
1455
else (empty_dom fm) in
1456
{proof…}
1457
let lemma all (x y: t) : unit
1458
requires { in_iv x a1 b1 /\ in_iv y a2 b2 }
1459
ensures { mem (fp_mul m x y) pp \/ mem (fp_mul m x y) pn
1460
\/ mem (fp_mul m x y) np \/ mem (fp_mul m x y) nn }
1461
= pt x a1 b1 sn1 sp1; pt y a2 b2 sn2 sp2 in
1462
{qed}
1463
union fm (union fm pp pn) (union fm np nn)

The products of [a1, b1] and l2

  Valid (92ms) (cvc5)
1466
let rec
mul_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a1 b1: t) (l2: list (t, t))
1467
: dom
1468
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt_u fm l2 }
1469
ensures { result.fm = fm }
1470
requires { specials4 fm minf pinf nz pz }
1471
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 /\ ok_u l2 }
1472
ensures { forall x y. in_iv x a1 b1 -> mem_u y l2 ->
1473
mem (fp_mul m x y) result }
1474
variant { l2 }
1475
= match l2 with
1476
| Nil -> (empty_dom fm)
1477
| Cons (a2, b2) r ->
1478
union fm (mul_iv fm minf pinf nz pz m a1 b1 a2 b2)
1479
(mul_iv_u fm minf pinf nz pz m a1 b1 r)
1480
end
1481
Valid (85ms) (cvc5)
1482
let rec
mul_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (l1 l2: list (t, t)) : dom
1483
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt_u fm l1 /\ fmt_u fm l2 }
1484
ensures { result.fm = fm }
1485
requires { specials4 fm minf pinf nz pz }
1486
requires { ok_u l1 /\ ok_u l2 }
1487
ensures { forall x y. mem_u x l1 -> mem_u y l2 -> mem (fp_mul m x y) result }
1488
variant { l1 }
1489
= match l1 with
1490
| Nil -> (empty_dom fm)
1491
| Cons (a1, b1) r ->
1492
union fm (mul_iv_u fm minf pinf nz pz m a1 b1 l2) (mul_u fm minf pinf nz pz m r l2)
1493
end

Sound: the products of the floats of d1 and d2 are in the result

  Valid (56ms) (cvc5)
1496
let
mul_dom
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (d1 d2: dom) : dom
1497
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d1.fm = fm /\ d2.fm = fm }
1498
ensures { result.fm = fm }
1499
requires { specials4 fm minf pinf nz pz }
1500
ensures { forall x y. mem x d1 -> mem y d2 -> mem (fp_mul m x y) result }
1501
= let r = mul_u fm minf pinf nz pz m d1.u d2.u in
1502
if d1.nan || d2.nan then union fm (nan_dom fm) r else r

Square root

Increasing (Facts.sqrt_le) from –0: NaN below.

  Valid (527ms) (colibri2 8) (cvc4 4) (cvc5 4) (split_vc) (depth 1)
1508
let
sqrt_iv
(ghost fm: format) (nz: t) (m: mode) (a b: t) : dom
1509
requires { fmt nz = fm /\ fmt a = fm /\ fmt b = fm }
1510
ensures { result.fm = fm }
1511
requires { at_index fm nz (-1) }
1512
requires { is_not_nan a /\ is_not_nan b /\ le_dz a b }
1513
ensures { forall x. in_iv x a b -> mem (fp_sqrt m x) result }
1514
= if lt_diff_zero b nz then begin
1515
{proof…}
1516
let lemma pt (x: t) : unit
1517
requires { in_iv x a b } ensures { is_nan (fp_sqrt m x) }
1518
= sqrt_special m x; index_range x; index_range b in
1519
{qed}
1520
(nan_dom fm)
1521
end else begin
1522
let lo = if le_diff_zero nz a then a else nz in
1523
{proof…}
1524
index_range a; index_range b;
1525
sqrt_special m lo; sqrt_special m b;
1526
sqrt_le m lo b;
1527
let lemma pt (x: t) : unit
1528
requires { in_iv x a b }
1529
ensures { if is_nan (fp_sqrt m x) then lt_dz a nz
1530
else in_iv (fp_sqrt m x) (fp_sqrt m lo) (fp_sqrt m b) }
1531
= sqrt_special m x; index_range x;
1532
if is_not_nan (fp_sqrt m x) then begin
1533
sqrt_le m lo x; sqrt_le m x b end in
1534
{qed}
1535
{ u = Cons (sqrt m lo, sqrt m b) Nil; nan = lt_diff_zero a nz; fm = fm }
1536
end
1537
Valid (183ms) (colibri2)
1538
let rec
sqrt_u
(ghost fm: format) (nz: t) (m: mode) (l: list (t, t)) : dom
1539
requires { fmt nz = fm /\ fmt_u fm l }
1540
ensures { result.fm = fm }
1541
requires { at_index fm nz (-1) /\ ok_u l }
1542
ensures { forall x. mem_u x l -> mem (fp_sqrt m x) result }
1543
variant { l }
1544
= match l with
1545
| Nil -> (empty_dom fm)
1546
| Cons (a, b) r -> union fm (sqrt_iv fm nz m a b) (sqrt_u fm nz m r)
1547
end

Sound: the square roots of the floats of d are in the result

  Valid (56ms) (cvc5)
1550
let
sqrt_dom
(ghost fm: format) (nz: t) (m: mode) (d: dom) : dom
1551
requires { fmt nz = fm /\ d.fm = fm }
1552
ensures { result.fm = fm }
1553
requires { at_index fm nz (-1) }
1554
ensures { forall x. mem x d -> mem (fp_sqrt m x) result }
1555
= let r = sqrt_u fm nz m d.u in
1556
if d.nan then union fm (nan_dom fm) r else r

Conversion

Conversion to the format fm' is increasing (Facts.convert_le): each interval goes to the interval of the conversions of its bounds.

  Valid (410ms) (cvc5)
1563
let rec
convert_u
(ghost fm: format) (fm': format) (m: mode) (l: list (t, t)) : dom
1564
requires { ok_u l /\ fmt_u fm l }
1565
ensures { result.fm = fm' }
1566
ensures { forall x. mem_u x l -> mem (fp_convert fm' m x) result }
1567
variant { l }
1568
= match l with
1569
| Nil -> empty_dom fm'
1570
| Cons (a, b) r ->
1571
{proof…} le_not_nan_any a b; {qed}
1572
let ca = convert fm' m a in
1573
let cb = convert fm' m b in
1574
{proof…}
1575
convert_not_nan fm' m a; convert_not_nan fm' m b; convert_le fm' m a b;
1576
{qed}
1577
let iv = interval fm' ca cb in
1578
{proof…}
1579
let lemma pt (x: t) : unit
1580
requires { in_iv x a b }
1581
ensures { mem (fp_convert fm' m x) iv }
1582
= convert_le fm' m a x; convert_le fm' m x b; convert_not_nan fm' m x in
1583
{qed}
1584
union fm' iv (convert_u fm fm' m r)
1585
end

The conversions of the floats of d

  Valid (43ms) (colibri2)
1588
let
convert_dom
(ghost fm: format) (fm': format) (m: mode) (d: dom) : dom
1589
requires { d.fm = fm }
1590
ensures { result.fm = fm' }
1591
ensures { forall x. mem x d -> mem (fp_convert fm' m x) result }
1592
= let r = convert_u fm fm' m d.u in
1593
if d.nan then union fm' (nan_dom fm') r else r

Rounding to an integral float

Increasing (Facts.rti_le), as the conversion.

  Valid (114ms) (cvc5)
1599
let rec
rti_u
(ghost fm: format) (m: mode) (l: list (t, t)) : dom
1600
requires { ok_u l /\ fmt_u fm l }
1601
ensures { result.fm = fm }
1602
ensures { forall x. mem_u x l -> mem (fp_rti m x) result }
1603
variant { l }
1604
= match l with
1605
| Nil -> empty_dom fm
1606
| Cons (a, b) r ->
1607
{proof…} le_not_nan_any a b; {qed}
1608
let ra = round_to_integral m a in
1609
let rb = round_to_integral m b in
1610
{proof…}
1611
rti_not_nan m a; rti_not_nan m b; rti_le m a b;
1612
{qed}
1613
let iv = interval fm ra rb in
1614
{proof…}
1615
let lemma pt (x: t) : unit
1616
requires { in_iv x a b }
1617
ensures { mem (fp_rti m x) iv }
1618
= rti_le m a x; rti_le m x b; rti_not_nan m x in
1619
{qed}
1620
union fm iv (rti_u fm m r)
1621
end

The roundings to an integral float of the floats of d

  Valid (36ms) (colibri2)
1624
let
rti_dom
(ghost fm: format) (m: mode) (d: dom) : dom
1625
requires { d.fm = fm }
1626
ensures { result.fm = fm }
1627
ensures { forall x. mem x d -> mem (fp_rti m x) result }
1628
= let r = rti_u fm m d.u in
1629
if d.nan then union fm (nan_dom fm) r else r

Division

As the multiplication, by quadrants (Facts.div_mag).

The quotients of x of sign px with |s1| <= |x| <= |l1| by y of sign py with |s2| <= |y| <= |l2|: from s1 / l2 to l1 / s2

  Valid (859ms) (colibri2 37) (cvc4 6) (cvc5 14) (split_vc 2) (depth 2)
1637
let
div_quad
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
1638
(px: bool) (s1 l1: t) (py: bool) (s2 l2: t) : dom
1639
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt s1 = fm /\ fmt l1 = fm /\ fmt s2 = fm /\ fmt l2 = fm }
1640
ensures { result.fm = fm }
1641
requires { specials4 fm minf pinf nz pz }
1642
requires { mag_le px s1 l1 /\ mag_le py s2 l2 }
1643
ensures { forall x y. mag_le px s1 x -> mag_le px x l1 ->
1644
mag_le py s2 y -> mag_le py y l2 -> mem (fp_div m x y) result }
1645
= let p = div m s1 l2 in
1646
let q = div m l1 s2 in
1647
let pos = if px then py else not py in
1648
{proof…}
1649
mag_le_sign px s1 l1; mag_le_sign py s2 l2;
1650
let lemma pt_sign (x y: t) : unit
1651
requires { mag_le px s1 x /\ mag_le px x l1 }
1652
requires { mag_le py s2 y /\ mag_le py y l2 }
1653
ensures { mul_zneg x y <-> not pos }
1654
ensures { is_zero x -> is_zero s1 }
1655
ensures { is_zero y -> is_zero s2 }
1656
ensures { is_infinity x -> is_infinity l1 }
1657
ensures { is_infinity y -> is_infinity l2 }
1658
ensures { is_zero l1 -> is_zero x }
1659
ensures { is_zero l2 -> is_zero y }
1660
ensures { is_infinity s1 -> is_infinity x }
1661
ensures { is_infinity s2 -> is_infinity y }
1662
= mag_le_sign px s1 x; mag_le_sign px x l1;
1663
mag_le_sign py s2 y; mag_le_sign py y l2;
1664
index_range x; index_range y; index_range s1; index_range s2;
1665
index_range l1; index_range l2 in
1666
{qed}
1667
match is_nan p, is_nan q with
1668
| True, True -> (* 0/0 or inf/inf at both ends *) (nan_dom fm)
1669
| True, False ->
1670
(* all the dividends infinite or all the divisors zeros *)
1671
let i = if pos then pinf else minf in
1672
{proof…}
1673
le_refl_any i;
1674
let lemma pt (x y: t) : unit
1675
requires { mag_le px s1 x /\ mag_le px x l1 }
1676
requires { mag_le py s2 y /\ mag_le py y l2 }
1677
ensures { is_nan (fp_div m x y) \/ fp_div m x y = i }
1678
= pt_sign x y; div_special m x y; div_special m s1 l2;
1679
div_special m l1 s2 in
1680
{qed}
1681
{ u = Cons (i, i) Nil; nan = true; fm = fm }
1682
| False, True ->
1683
(* all the dividends zeros or all the divisors infinite *)
1684
let z = if pos then pz else nz in
1685
{proof…}
1686
le_refl_any z;
1687
let lemma pt (x y: t) : unit
1688
requires { mag_le px s1 x /\ mag_le px x l1 }
1689
requires { mag_le py s2 y /\ mag_le py y l2 }
1690
ensures { is_nan (fp_div m x y) \/ fp_div m x y = z }
1691
= pt_sign x y; div_special m x y; div_special m s1 l2;
1692
div_special m l1 s2;
1693
let r = fp_div m x y in
1694
if is_not_nan r then begin
1695
index_range r;
1696
assert { r = z by of_index fm (index r) = of_index fm (index z) }
1697
end in
1698
{qed}
1699
{ u = Cons (z, z) Nil; nan = true; fm = fm }
1700
| False, False ->
1701
{proof…}
1702
div_mag m px py s1 l1 s2 l2;
1703
let lemma pt (x y: t) : unit
1704
requires { mag_le px s1 x /\ mag_le px x l1 }
1705
requires { mag_le py s2 y /\ mag_le py y l2 }
1706
requires { is_not_nan (fp_div m x y) }
1707
ensures { if pos then in_iv (fp_div m x y) p q
1708
else in_iv (fp_div m x y) q p }
1709
= div_mag m px py s1 x y l2; div_mag m px py x l1 s2 y in
1710
let lemma pn (x y: t) : unit
1711
requires { mag_le px s1 x /\ mag_le px x l1 }
1712
requires { mag_le py s2 y /\ mag_le py y l2 }
1713
requires { is_nan (fp_div m x y) }
1714
ensures { (is_zero s1 /\ is_zero s2) \/ (is_infinity l1 /\ is_infinity l2) }
1715
= pt_sign x y; div_special m x y in
1716
{qed}
1717
{ u = Cons (if pos then (p, q) else (q, p)) Nil;
1718
nan = (is_zero_f fm nz pz s1 && is_zero_f fm nz pz s2)
1719
|| (is_inf_f fm minf pinf l1 && is_inf_f fm minf pinf l2); fm = fm }
1720
end

The quotients of the box [a1, b1] x [a2, b2], by the four quadrants

  Valid (381ms) (colibri2 50) (cvc5 6) (split_vc 13) (depth 4)
1723
let
div_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a1 b1 a2 b2: t) : dom
1724
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt a2 = fm /\ fmt b2 = fm }
1725
ensures { result.fm = fm }
1726
requires { specials4 fm minf pinf nz pz }
1727
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 }
1728
requires { is_not_nan a2 /\ is_not_nan b2 /\ le_dz a2 b2 }
1729
ensures { forall x y. in_iv x a1 b1 -> in_iv y a2 b2 ->
1730
mem (fp_div m x y) result }
1731
= {proof…}
1732
let lemma sign_split (x: t) : unit
1733
requires { is_not_nan x /\ fmt x = fm }
1734
ensures { is_positive x <-> le_dz pz x }
1735
ensures { not is_positive x <-> le_dz x nz }
1736
= index_range x in
1737
{qed}
1738
(* the negative part [a, min b -0], the positive part [max a +0, b] *)
1739
let n1 = le_diff_zero a1 nz in
1740
let p1 = le_diff_zero pz b1 in
1741
let n2 = le_diff_zero a2 nz in
1742
let p2 = le_diff_zero pz b2 in
1743
let sn1 = if le_diff_zero b1 nz then b1 else nz in
1744
let sp1 = if le_diff_zero pz a1 then a1 else pz in
1745
let sn2 = if le_diff_zero b2 nz then b2 else nz in
1746
let sp2 = if le_diff_zero pz a2 then a2 else pz in
1747
{proof…}
1748
let lemma pt (x: t) (a b sn sp: t) : unit
1749
requires { is_not_nan a /\ is_not_nan b /\ in_iv x a b /\ fmt a = fm }
1750
requires { sn = (if le_dz b nz then b else nz) }
1751
requires { sp = (if le_dz pz a then a else pz) }
1752
ensures { is_positive x -> le_dz pz b /\ mag_le true sp x /\ mag_le true x b }
1753
ensures { not is_positive x ->
1754
le_dz a nz /\ mag_le false sn x /\ mag_le false x a }
1755
= index_range x; index_range a; index_range b in
1756
let lemma ends (a b sn sp: t) : unit
1757
requires { is_not_nan a /\ is_not_nan b /\ le_dz a b /\ fmt a = fm }
1758
requires { sn = (if le_dz b nz then b else nz) }
1759
requires { sp = (if le_dz pz a then a else pz) }
1760
ensures { le_dz a nz -> mag_le false sn a }
1761
ensures { le_dz pz b -> mag_le true sp b }
1762
= index_range a; index_range b in
1763
ends a1 b1 sn1 sp1; ends a2 b2 sn2 sp2;
1764
{qed}
1765
let pp = if p1 && p2 then div_quad fm minf pinf nz pz m true sp1 b1 true sp2 b2
1766
else (empty_dom fm) in
1767
let pn = if p1 && n2 then div_quad fm minf pinf nz pz m true sp1 b1 false sn2 a2
1768
else (empty_dom fm) in
1769
let np = if n1 && p2 then div_quad fm minf pinf nz pz m false sn1 a1 true sp2 b2
1770
else (empty_dom fm) in
1771
let nn = if n1 && n2 then div_quad fm minf pinf nz pz m false sn1 a1 false sn2 a2
1772
else (empty_dom fm) in
1773
{proof…}
1774
let lemma all (x y: t) : unit
1775
requires { in_iv x a1 b1 /\ in_iv y a2 b2 }
1776
ensures { mem (fp_div m x y) pp \/ mem (fp_div m x y) pn
1777
\/ mem (fp_div m x y) np \/ mem (fp_div m x y) nn }
1778
= pt x a1 b1 sn1 sp1; pt y a2 b2 sn2 sp2 in
1779
{qed}
1780
union fm (union fm pp pn) (union fm np nn)

The quotients of [a1, b1] and l2

  Valid (309ms) (cvc5)
1783
let rec
div_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a1 b1: t) (l2: list (t, t))
1784
: dom
1785
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt a1 = fm /\ fmt b1 = fm /\ fmt_u fm l2 }
1786
ensures { result.fm = fm }
1787
requires { specials4 fm minf pinf nz pz }
1788
requires { is_not_nan a1 /\ is_not_nan b1 /\ le_dz a1 b1 /\ ok_u l2 }
1789
ensures { forall x y. in_iv x a1 b1 -> mem_u y l2 ->
1790
mem (fp_div m x y) result }
1791
variant { l2 }
1792
= match l2 with
1793
| Nil -> (empty_dom fm)
1794
| Cons (a2, b2) r ->
1795
union fm (div_iv fm minf pinf nz pz m a1 b1 a2 b2)
1796
(div_iv_u fm minf pinf nz pz m a1 b1 r)
1797
end
1798
Valid (325ms) (cvc5)
1799
let rec
div_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (l1 l2: list (t, t)) : dom
1800
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ fmt_u fm l1 /\ fmt_u fm l2 }
1801
ensures { result.fm = fm }
1802
requires { specials4 fm minf pinf nz pz }
1803
requires { ok_u l1 /\ ok_u l2 }
1804
ensures { forall x y. mem_u x l1 -> mem_u y l2 -> mem (fp_div m x y) result }
1805
variant { l1 }
1806
= match l1 with
1807
| Nil -> (empty_dom fm)
1808
| Cons (a1, b1) r ->
1809
union fm (div_iv_u fm minf pinf nz pz m a1 b1 l2) (div_u fm minf pinf nz pz m r l2)
1810
end

Sound: the quotients of the floats of d1 and d2 are in the result

  Valid (89ms) (cvc5)
1813
let
div_dom
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (d1 d2: dom) : dom
1814
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d1.fm = fm /\ d2.fm = fm }
1815
ensures { result.fm = fm }
1816
requires { specials4 fm minf pinf nz pz }
1817
ensures { forall x y. mem x d1 -> mem y d2 -> mem (fp_div m x y) result }
1818
= let r = div_u fm minf pinf nz pz m d1.u d2.u in
1819
if d1.nan || d2.nan then union fm (nan_dom fm) r else r
Backward propagation of the comparisons

Exact: for the comparison x op y with x in d, the domain of the y such that some x of d makes it true (cmp_true_*, None when there is none) or false (cmp_false_*). The comparisons are those of IEEE 754 (fp_le, fp_lt, fp_eq: –0 = +0, NaN unordered).

  Valid (23ms) (colibri2)
1828
let
is_pz
(ghost fm: format) (pz x: t) : bool
1829
requires { fmt pz = fm /\ fmt x = fm }
1830
requires { at_index fm pz 0 /\ is_not_nan x }
1831
ensures { result <-> index x = 0 }
1832
= le_diff_zero pz x && le_diff_zero x pz
1833
Valid (24ms) (colibri2)
1834
let
is_nz
(ghost fm: format) (nz x: t) : bool
1835
requires { fmt nz = fm /\ fmt x = fm }
1836
requires { at_index fm nz (-1) /\ is_not_nan x }
1837
ensures { result <-> index x = -1 }
1838
= le_diff_zero nz x && le_diff_zero x nz
1839
Valid (17ms) (colibri2)
1840
let
first
(ghost fm: format) (d: dom) : t
1841
requires { d.fm = fm }
1842
ensures { fmt result = fm }
1843
requires { d.u <> Nil }
1844
ensures { mem_u result d.u /\ is_not_nan result }
1845
ensures { forall v. mem_u v d.u -> le_dz result v }
1846
= let r = try min_f fm d with Empty -> absurd end in
1847
{proof…} mem_u_not_nan r d.u; {qed}
1848
r
1849
Valid (15ms) (colibri2)
1850
let
last
(ghost fm: format) (d: dom) : t
1851
requires { d.fm = fm }
1852
ensures { fmt result = fm }
1853
requires { d.u <> Nil }
1854
ensures { mem_u result d.u /\ is_not_nan result }
1855
ensures { forall v. mem_u v d.u -> le_dz v result }
1856
= let r = try max_f fm d with Empty -> absurd end in
1857
{proof…} mem_u_not_nan r d.u; {qed}
1858
r

x <= y

  Valid (814ms) (colibri2 15) (cvc4 2) (cvc5 4) (split_vc 5) (depth 3)
1861
let
cmp_true_le
(ghost fm: format) (nz pz pinf: t) (d: dom) : option dom
1862
requires { fmt nz = fm /\ fmt pz = fm /\ fmt pinf = fm /\ d.fm = fm }
1863
ensures { match result with Some r -> r.fm = fm | None -> true end }
1864
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ pinf = (plus_infinity fm) }
1865
ensures { match result with
1866
| None -> forall x y. mem x d -> not fp_le x y
1867
| Some r -> not is_bot r /\ forall y. mem y r <-> exists x. mem x d /\ fp_le x y
1868
end }
1869
= match d.u with
1870
| Nil -> None
1871
| Cons _ _ ->
1872
let f1 = first fm d in
1873
let f1' = if is_pz fm pz f1 then nz else f1 in
1874
{proof…}
1875
index_range f1; index_range pinf;
1876
let lemma pt (y: t) : unit
1877
ensures { (is_not_nan y /\ in_iv y f1' pinf) <->
1878
exists x. mem x d /\ fp_le x y }
1879
= if is_not_nan y && pure { fmt y = fm } then begin
1880
index_range y; if pure { index f1 = 0 /\ index y = -1 } then
1881
assert { mem f1 d /\ fp_le f1 y }
1882
else if pure { index f1' <= index y } then
1883
assert { mem f1 d /\ fp_le f1 y }
1884
end;
1885
if pure { exists x. mem x d /\ fp_le x y } then begin
1886
let x = any t ensures { mem result d /\ fp_le result y } in
1887
index_range x
1888
end in
1889
{qed}
1890
Some (interval fm f1' pinf)
1891
end

x < y

  Valid (623ms) (colibri2 24) (cvc4) (cvc5 7) (split_vc 4) (depth 2)
1894
let
cmp_true_lt
(ghost fm: format) (nz pz pinf: t) (d: dom) : option dom
1895
requires { fmt nz = fm /\ fmt pz = fm /\ fmt pinf = fm /\ d.fm = fm }
1896
ensures { match result with Some r -> r.fm = fm | None -> true end }
1897
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ pinf = (plus_infinity fm) }
1898
ensures { match result with
1899
| None -> forall x y. mem x d -> not fp_lt x y
1900
| Some r -> not is_bot r /\ forall y. mem y r <-> exists x. mem x d /\ fp_lt x y
1901
end }
1902
= match d.u with
1903
| Nil -> None
1904
| Cons _ _ ->
1905
let f1 = first fm d in
1906
if le_diff_zero pinf f1 then begin
1907
{proof…}
1908
let lemma pt (x y: t) : unit
1909
requires { mem x d } ensures { not fp_lt x y }
1910
= if not (is_nan x) && not (is_nan y) then begin
1911
index_range x; index_range y; index_range f1; index_range pinf
1912
end in
1913
{qed}
1914
None
1915
end else begin
1916
let f1' = if is_nz fm nz f1 then pz else f1 in
1917
{proof…}
1918
index_range f1; index_range pinf;
1919
{qed}
1920
let s = succ_diff_zero f1' in
1921
{proof…}
1922
succ_gt f1'; index_range s;
1923
succ_least f1' pinf; let lemma pt (y: t) : unit
1924
ensures { (is_not_nan y /\ in_iv y s pinf) <->
1925
exists x. mem x d /\ fp_lt x y }
1926
= if is_not_nan y && pure { fmt y = fm } then begin
1927
index_range y; if pure { index s <= index y } then
1928
assert { mem f1 d /\ fp_lt f1 y }
1929
end;
1930
if pure { exists x. mem x d /\ fp_lt x y } then begin
1931
let x = any t ensures { mem result d /\ fp_lt result y } in
1932
index_range x; index_range y;
1933
assert { lt_dz f1' y };
1934
succ_least f1' y
1935
end in
1936
{qed}
1937
Some (interval fm s pinf)
1938
end
1939
end

x >= y, that is y <= x

  Valid (787ms) (colibri2 17) (cvc5 5) (split_vc 5) (depth 3)
1942
let
cmp_true_ge
(ghost fm: format) (nz pz minf: t) (d: dom) : option dom
1943
requires { fmt nz = fm /\ fmt pz = fm /\ fmt minf = fm /\ d.fm = fm }
1944
ensures { match result with Some r -> r.fm = fm | None -> true end }
1945
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ minf = (minus_infinity fm) }
1946
ensures { match result with
1947
| None -> forall x y. mem x d -> not fp_le y x
1948
| Some r -> not is_bot r /\ forall y. mem y r <-> exists x. mem x d /\ fp_le y x
1949
end }
1950
= match d.u with
1951
| Nil -> None
1952
| Cons _ _ ->
1953
let f2 = last fm d in
1954
let f2' = if is_nz fm nz f2 then pz else f2 in
1955
{proof…}
1956
index_range f2; index_range minf;
1957
let lemma pt (y: t) : unit
1958
ensures { (is_not_nan y /\ in_iv y minf f2') <->
1959
exists x. mem x d /\ fp_le y x }
1960
= if is_not_nan y && pure { fmt y = fm } then begin
1961
index_range y; if pure { index f2 = -1 /\ index y = 0 } then
1962
assert { mem f2 d /\ fp_le y f2 }
1963
else if pure { index y <= index f2' } then
1964
assert { mem f2 d /\ fp_le y f2 }
1965
end;
1966
if pure { exists x. mem x d /\ fp_le y x } then begin
1967
let x = any t ensures { mem result d /\ fp_le y result } in
1968
index_range x
1969
end in
1970
{qed}
1971
Some (interval fm minf f2')
1972
end

x > y, that is y < x

  Valid (872ms) (colibri2 24) (cvc4) (cvc5 7) (split_vc 4) (depth 2)
1975
let
cmp_true_gt
(ghost fm: format) (nz pz minf: t) (d: dom) : option dom
1976
requires { fmt nz = fm /\ fmt pz = fm /\ fmt minf = fm /\ d.fm = fm }
1977
ensures { match result with Some r -> r.fm = fm | None -> true end }
1978
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ minf = (minus_infinity fm) }
1979
ensures { match result with
1980
| None -> forall x y. mem x d -> not fp_lt y x
1981
| Some r -> not is_bot r /\ forall y. mem y r <-> exists x. mem x d /\ fp_lt y x
1982
end }
1983
= match d.u with
1984
| Nil -> None
1985
| Cons _ _ ->
1986
let f2 = last fm d in
1987
if le_diff_zero f2 minf then begin
1988
{proof…}
1989
let lemma pt (x y: t) : unit
1990
requires { mem x d } ensures { not fp_lt y x }
1991
= if not (is_nan x) && not (is_nan y) then begin
1992
index_range x; index_range y; index_range f2; index_range minf
1993
end in
1994
{qed}
1995
None
1996
end else begin
1997
let f2' = if is_pz fm pz f2 then nz else f2 in
1998
{proof…}
1999
index_range f2; index_range minf;
2000
{qed}
2001
let s = pred_diff_zero f2' in
2002
{proof…}
2003
pred_lt f2'; index_range s;
2004
pred_greatest f2' minf; let lemma pt (y: t) : unit
2005
ensures { (is_not_nan y /\ in_iv y minf s) <->
2006
exists x. mem x d /\ fp_lt y x }
2007
= if is_not_nan y && pure { fmt y = fm } then begin
2008
index_range y; if pure { index y <= index s } then
2009
assert { mem f2 d /\ fp_lt y f2 }
2010
end;
2011
if pure { exists x. mem x d /\ fp_lt y x } then begin
2012
let x = any t ensures { mem result d /\ fp_lt y result } in
2013
index_range x; index_range y;
2014
assert { lt_dz y f2' };
2015
pred_greatest f2' y
2016
end in
2017
{qed}
2018
Some (interval fm minf s)
2019
end
2020
end

x = y

  Valid (606ms) (colibri2 18) (cvc4 2) (cvc5 3) (split_vc 3) (depth 2)
2023
let
cmp_true_eq
(ghost fm: format) (nz pz: t) (d: dom) : option dom
2024
requires { fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2025
ensures { match result with Some r -> r.fm = fm | None -> true end }
2026
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
2027
ensures { match result with
2028
| None -> forall x y. mem x d -> not fp_eq x y
2029
| Some r -> not is_bot r /\ forall y. mem y r <-> exists x. mem x d /\ fp_eq x y
2030
end }
2031
= let l = if meets fm nz pz d then union_u fm d.u (Cons (nz, pz) Nil) else d.u in
2032
{proof…}
2033
le_refl_any nz; le_refl_any pz; index_range nz;
2034
let lemma pt (y: t) : unit
2035
ensures { mem_u y l <-> exists x. mem x d /\ fp_eq x y }
2036
= if pure { mem_u y l } then begin
2037
mem_u_not_nan y l; index_range y;
2038
if pure { mem_u y d.u } then begin
2039
mem_u_not_nan y d.u; assert { mem y d /\ fp_eq y y }
2040
end else begin
2041
let x = any t ensures { mem result d /\ is_not_nan result /\
2042
index nz <= index result <= index pz } in
2043
assert { fp_eq x y }
2044
end
2045
end;
2046
if pure { exists x. mem x d /\ fp_eq x y } then begin
2047
let x = any t ensures { mem result d /\ fp_eq result y } in
2048
if is_nan x then absurd;
2049
index_range x; mem_u_not_nan x d.u;
2050
if pure { is_zero x /\ is_zero y } then
2051
assert { index nz <= index y <= index pz /\ index nz <= index x <= index pz }
2052
end in
2053
{qed}
2054
match l with
2055
| Nil -> None
2056
| Cons _ _ -> Some { u = l; nan = false; fm = fm }
2057
end

not (x <= y): NaN, or below the largest x

  Valid (584ms) (colibri2 29) (cvc4 2) (cvc5 9) (split_vc 8) (depth 5)
2060
let
cmp_false_le
(ghost fm: format) (minf pinf nz pz: t) (d: dom) : dom
2061
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2062
ensures { result.fm = fm }
2063
requires { specials4 fm minf pinf nz pz }
2064
ensures { forall y. mem y result <->
2065
fmt y = fm /\ exists x. mem x d /\ not fp_le x y }
2066
= if d.nan then begin
2067
let r = top fm minf pinf in
2068
{proof…}
2069
let lemma pt (y: t) : unit
2070
requires { mem y r }
2071
ensures { exists x. mem x d /\ not fp_le x y }
2072
= assert { mem (not_a_number fm) d /\ not fp_le (not_a_number fm) y } in
2073
{qed}
2074
r
2075
end else match d.u with
2076
| Nil -> (empty_dom fm)
2077
| Cons _ _ ->
2078
let f2 = last fm d in
2079
let f2' = if is_pz fm pz f2 then nz else f2 in
2080
{proof…}
2081
index_range f2; index_range minf; index_range pinf;
2082
{qed}
2083
if le_diff_zero f2' minf then begin
2084
{proof…}
2085
let lemma pt (y: t) : unit
2086
requires { fmt y = fm }
2087
ensures { is_nan y <-> exists x. mem x d /\ not fp_le x y }
2088
= if is_nan y then assert { mem f2 d /\ not fp_le f2 y };
2089
if pure { exists x. mem x d /\ not fp_le x y } then begin
2090
let x = any t ensures { mem result d /\ not fp_le result y } in
2091
mem_u_not_nan x d.u; index_range x;
2092
if not (is_nan y) then index_range y
2093
end in
2094
{qed}
2095
(nan_dom fm)
2096
end else begin
2097
let p = pred_diff_zero f2' in
2098
{proof…}
2099
pred_lt f2'; index_range p; pred_greatest f2' minf;
2100
let lemma pt (y: t) : unit
2101
requires { fmt y = fm }
2102
ensures { (is_nan y \/ in_iv y minf p) <->
2103
exists x. mem x d /\ not fp_le x y }
2104
= if is_nan y then assert { mem f2 d /\ not fp_le f2 y }
2105
else begin
2106
index_range y;
2107
if pure { in_iv y minf p } then
2108
assert { mem f2 d /\ not fp_le f2 y };
2109
if pure { exists x. mem x d /\ not fp_le x y } then begin
2110
let x = any t ensures { mem result d /\ not fp_le result y } in
2111
mem_u_not_nan x d.u; index_range x;
2112
assert { lt_dz y f2' };
2113
pred_greatest f2' y
2114
end
2115
end in
2116
{qed}
2117
{ u = Cons (minf, p) Nil; nan = true; fm = fm }
2118
end
2119
end

not (x < y): NaN, or up to the largest x

  Valid (507ms) (colibri2 21) (cvc4 3) (cvc5 3) (split_vc 7) (depth 5)
2122
let
cmp_false_lt
(ghost fm: format) (minf pinf nz pz: t) (d: dom) : dom
2123
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2124
ensures { result.fm = fm }
2125
requires { specials4 fm minf pinf nz pz }
2126
ensures { forall y. mem y result <->
2127
fmt y = fm /\ exists x. mem x d /\ not fp_lt x y }
2128
= if d.nan then begin
2129
let r = top fm minf pinf in
2130
{proof…}
2131
let lemma pt (y: t) : unit
2132
requires { mem y r }
2133
ensures { exists x. mem x d /\ not fp_lt x y }
2134
= assert { mem (not_a_number fm) d /\ not fp_lt (not_a_number fm) y } in
2135
{qed}
2136
r
2137
end else match d.u with
2138
| Nil -> (empty_dom fm)
2139
| Cons _ _ ->
2140
let f2 = last fm d in
2141
let f2' = if is_nz fm nz f2 then pz else f2 in
2142
{proof…}
2143
index_range f2; index_range minf; index_range pinf;
2144
let lemma pt (y: t) : unit
2145
requires { fmt y = fm }
2146
ensures { (is_nan y \/ in_iv y minf f2') <->
2147
exists x. mem x d /\ not fp_lt x y }
2148
= if is_nan y then assert { mem f2 d /\ not fp_lt f2 y }
2149
else begin
2150
index_range y;
2151
if pure { in_iv y minf f2' } then
2152
assert { mem f2 d /\ not fp_lt f2 y };
2153
if pure { exists x. mem x d /\ not fp_lt x y } then begin
2154
let x = any t ensures { mem result d /\ not fp_lt result y } in
2155
mem_u_not_nan x d.u; index_range x
2156
end
2157
end in
2158
{qed}
2159
{ u = Cons (minf, f2') Nil; nan = true; fm = fm }
2160
end

not (x >= y), that is not (y <= x): NaN, or above the least x

  Valid (1.1s) (colibri2 29) (cvc4 3) (cvc5 8) (split_vc 8) (depth 5)
2163
let
cmp_false_ge
(ghost fm: format) (minf pinf nz pz: t) (d: dom) : dom
2164
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2165
ensures { result.fm = fm }
2166
requires { specials4 fm minf pinf nz pz }
2167
ensures { forall y. mem y result <->
2168
fmt y = fm /\ exists x. mem x d /\ not fp_le y x }
2169
= if d.nan then begin
2170
let r = top fm minf pinf in
2171
{proof…}
2172
let lemma pt (y: t) : unit
2173
requires { mem y r }
2174
ensures { exists x. mem x d /\ not fp_le y x }
2175
= assert { mem (not_a_number fm) d /\ not fp_le y (not_a_number fm) } in
2176
{qed}
2177
r
2178
end else match d.u with
2179
| Nil -> (empty_dom fm)
2180
| Cons _ _ ->
2181
let f1 = first fm d in
2182
let f1' = if is_nz fm nz f1 then pz else f1 in
2183
{proof…}
2184
index_range f1; index_range minf; index_range pinf;
2185
{qed}
2186
if le_diff_zero pinf f1' then begin
2187
{proof…}
2188
let lemma pt (y: t) : unit
2189
requires { fmt y = fm }
2190
ensures { is_nan y <-> exists x. mem x d /\ not fp_le y x }
2191
= if is_nan y then assert { mem f1 d /\ not fp_le y f1 };
2192
if pure { exists x. mem x d /\ not fp_le y x } then begin
2193
let x = any t ensures { mem result d /\ not fp_le y result } in
2194
mem_u_not_nan x d.u; index_range x;
2195
if not (is_nan y) then index_range y
2196
end in
2197
{qed}
2198
(nan_dom fm)
2199
end else begin
2200
let s = succ_diff_zero f1' in
2201
{proof…}
2202
succ_gt f1'; index_range s; succ_least f1' pinf;
2203
let lemma pt (y: t) : unit
2204
requires { fmt y = fm }
2205
ensures { (is_nan y \/ in_iv y s pinf) <->
2206
exists x. mem x d /\ not fp_le y x }
2207
= if is_nan y then assert { mem f1 d /\ not fp_le y f1 }
2208
else begin
2209
index_range y;
2210
if pure { in_iv y s pinf } then
2211
assert { mem f1 d /\ not fp_le y f1 };
2212
if pure { exists x. mem x d /\ not fp_le y x } then begin
2213
let x = any t ensures { mem result d /\ not fp_le y result } in
2214
mem_u_not_nan x d.u; index_range x;
2215
assert { lt_dz f1' y };
2216
succ_least f1' y
2217
end
2218
end in
2219
{qed}
2220
{ u = Cons (s, pinf) Nil; nan = true; fm = fm }
2221
end
2222
end

not (x > y), that is not (y < x): NaN, or from the least x

  Valid (876ms) (colibri2 23) (cvc4 2) (cvc5 2) (split_vc 7) (depth 5)
2225
let
cmp_false_gt
(ghost fm: format) (minf pinf nz pz: t) (d: dom) : dom
2226
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2227
ensures { result.fm = fm }
2228
requires { specials4 fm minf pinf nz pz }
2229
ensures { forall y. mem y result <->
2230
fmt y = fm /\ exists x. mem x d /\ not fp_lt y x }
2231
= if d.nan then begin
2232
let r = top fm minf pinf in
2233
{proof…}
2234
let lemma pt (y: t) : unit
2235
requires { mem y r }
2236
ensures { exists x. mem x d /\ not fp_lt y x }
2237
= assert { mem (not_a_number fm) d /\ not fp_lt y (not_a_number fm) } in
2238
{qed}
2239
r
2240
end else match d.u with
2241
| Nil -> (empty_dom fm)
2242
| Cons _ _ ->
2243
let f1 = first fm d in
2244
let f1' = if is_pz fm pz f1 then nz else f1 in
2245
{proof…}
2246
index_range f1; index_range minf; index_range pinf;
2247
let lemma pt (y: t) : unit
2248
requires { fmt y = fm }
2249
ensures { (is_nan y \/ in_iv y f1' pinf) <->
2250
exists x. mem x d /\ not fp_lt y x }
2251
= if is_nan y then assert { mem f1 d /\ not fp_lt y f1 }
2252
else begin
2253
index_range y;
2254
if pure { in_iv y f1' pinf } then
2255
assert { mem f1 d /\ not fp_lt y f1 };
2256
if pure { exists x. mem x d /\ not fp_lt y x } then begin
2257
let x = any t ensures { mem result d /\ not fp_lt y result } in
2258
mem_u_not_nan x d.u; index_range x
2259
end
2260
end in
2261
{qed}
2262
{ u = Cons (f1', pinf) Nil; nan = true; fm = fm }
2263
end

not (x = y): all the floats, but when the floats of d are one value of IEEE 754 (one float, or the two zeros)

  Valid (524ms) (colibri2 28) (cvc4 5) (cvc5 8) (split_vc 3) (depth 3)
2267
let
cmp_false_eq
(ghost fm: format) (minf pinf nz pz: t) (d: dom) : dom
2268
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm /\ d.fm = fm }
2269
ensures { result.fm = fm }
2270
requires { specials4 fm minf pinf nz pz }
2271
ensures { forall y. mem y result <->
2272
fmt y = fm /\ exists x. mem x d /\ not fp_eq x y }
2273
= if d.nan then begin
2274
let r = top fm minf pinf in
2275
{proof…}
2276
let lemma pt (y: t) : unit
2277
requires { mem y r }
2278
ensures { exists x. mem x d /\ not fp_eq x y }
2279
= assert { mem (not_a_number fm) d /\ not fp_eq (not_a_number fm) y } in
2280
{qed}
2281
r
2282
end else match d.u with
2283
| Nil -> (empty_dom fm)
2284
| Cons _ _ ->
2285
let f1 = first fm d in
2286
let f2 = last fm d in
2287
{proof…}
2288
index_range f1; index_range f2; index_range nz;
2289
{qed}
2290
let z1 = is_zero_f fm nz pz f1 in
2291
let z2 = is_zero_f fm nz pz f2 in
2292
if le_diff_zero f2 f1 || (z1 && z2) then begin
2293
let lo = if z1 then nz else f1 in
2294
let hi = if z2 then pz else f2 in
2295
{proof…}
2296
let lemma pt (y: t) : unit
2297
requires { fmt y = fm }
2298
ensures { not (is_not_nan y /\ in_iv y lo hi) <->
2299
exists x. mem x d /\ not fp_eq x y }
2300
= if is_nan y then assert { mem f1 d /\ not fp_eq f1 y }
2301
else begin
2302
index_range y;
2303
if pure { not in_iv y lo hi } then
2304
assert { mem f1 d /\ not fp_eq f1 y };
2305
if pure { exists x. mem x d /\ not fp_eq x y } then begin
2306
let x = any t ensures { mem result d /\ not fp_eq result y } in
2307
mem_u_not_nan x d.u; index_range x;
2308
if pure { in_iv y lo hi } then begin
2309
assert { index f1 <= index x <= index f2 };
2310
if z1 && z2 then assert { is_zero x /\ is_zero y }
2311
else begin
2312
assert { x = f1 by of_index fm (index x) = of_index fm (index f1) };
2313
assert { y = f1 by of_index fm (index y) = of_index fm (index f1) }
2314
end
2315
end
2316
end
2317
end in
2318
{qed}
2319
match complement fm minf pinf (interval fm lo hi) with
2320
| None -> absurd
2321
| Some c -> c
2322
end
2323
end else begin
2324
let r = top fm minf pinf in
2325
{proof…}
2326
let lemma pt (y: t) : unit
2327
requires { mem y r }
2328
ensures { exists x. mem x d /\ not fp_eq x y }
2329
= if pure { fp_eq f1 y } then begin
2330
assert { not fp_eq f2 y };
2331
assert { mem f2 d }
2332
end else assert { mem f1 d } in
2333
{qed}
2334
r
2335
end
2336
end
Backward conversion

The x of the format fm whose conversion to the format fm' is in [a, b] are above the conversion down to fm of the predecessor of a and below the conversion up of the successor of b (Facts.convert_lower, convert_upper). Sound for any mode, from the monotonicity of the rounding only: the floats of fm between the predecessor of a and the preimage of a by the rounding are kept.

x is –inf (resp. +inf) of its format

  Valid (16ms) (colibri2)
2349
let
is_minf
(ghost fm: format) (minf x: t) : bool
2350
requires { fmt minf = fm /\ fmt x = fm /\ minf = minus_infinity fm /\ is_not_nan x }
2351
ensures { result <-> is_minus_infinity x }
2352
= {proof…}
2353
if pure { is_minus_infinity x } then begin
2354
minus_infinity_unique x; le_refl x
2355
end else if pure { le_dz x minf } then begin
2356
minus_infinity_lt x; lt_not_le minf x
2357
end;
2358
{qed}
2359
le_diff_zero x minf
2360
Valid (21ms) (colibri2)
2361
let
is_pinf
(ghost fm: format) (pinf x: t) : bool
2362
requires { fmt pinf = fm /\ fmt x = fm /\ pinf = plus_infinity fm /\ is_not_nan x }
2363
ensures { result <-> is_plus_infinity x }
2364
= {proof…}
2365
if pure { is_plus_infinity x } then begin
2366
plus_infinity_unique x; le_refl x
2367
end else if pure { le_dz pinf x } then begin
2368
lt_plus_infinity x; lt_not_le x pinf
2369
end;
2370
{qed}
2371
le_diff_zero pinf x
Backward addition

The x such that x + y is in [a, b] for some y in [c, d]: above pa - d rounded down, below sb - c rounded up, where pa is the float below a in value (below –0 when a is a zero) and sb the float above b (Facts.add_lower, add_upper). Sound for any mode, from the monotonicity of the rounding only.

Thresholds in the format with one more bit

thr_lo fm' m a is the float t of the format fm' with one more bit such that the extended reals that round to a or above are at or above t (Intf.round_f_ge_thr), with a zero a taken as –0; thr_hi is the symmetric one, a zero b taken as +0.

  Valid (462ms) (colibri2 16) (cvc4) (cvc5 9) (split_vc 2) (depth 2)
2387
let
thr_lo
(ghost fm: format) (fm': format) (nz pz: t) (m: mode) (a: t) : t
2388
requires { widens fm fm' /\ fmt a = fm /\ fmt nz = fm /\ fmt pz = fm }
2389
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
2390
requires { is_not_nan a /\ not is_minus_infinity a }
2391
ensures { fmt result = fm' /\ is_not_nan result }
2392
ensures { forall z e. le_dz a (round_f fm m z e) -> ele (ext result) e }
2393
= let a0 = if is_zero_f fm nz pz a then nz else a in
2394
{proof…}
2395
index_range a;
2396
assert { index a <> - nb_sign fm };
2397
assert { le_dz a0 a /\ is_not_nan a0 /\ fmt a0 = fm };
2398
let ghost i = index a0 in
2399
assert { i <> 0 /\ - nb_sign fm < i < nb_sign fm };
2400
convert_widen fm' rtn a0; widen_nb_sign fm fm';
2401
let ghost w0 = windex i in
2402
assert { - nb_sign fm' + 2 <= w0 < nb_sign fm' };
2403
assert { w0 + lo_off m i < nb_sign fm' };
2404
of_index_range fm' w0; of_index_range fm' (w0 - 1); of_index_range fm' (w0 - 2);
2405
{qed}
2406
let w = convert fm' rtn a0 in
2407
let t = match m with
2408
| RTN -> w
2409
| RTP -> pred_diff_zero (pred_diff_zero w)
2410
| RTZ -> if le_diff_zero pz a0 then w else pred_diff_zero (pred_diff_zero w)
2411
| RNE | RNA -> pred_diff_zero w
2412
end in
2413
{proof…}
2414
assert { t = of_index fm' (w0 + lo_off m i) };
2415
of_index_range fm' (w0 + lo_off m i);
2416
let lemma thr (z: bool) (e: ereal) : unit
2417
requires { le_dz a (round_f fm m z e) } ensures { ele (ext t) e }
2418
= le_trans a0 a (round_f fm m z e) in
2419
{qed}
2420
t
2421
Valid (708ms) (colibri2 19) (cvc4) (cvc5 10) (split_vc 3) (depth 2)
2422
let
thr_hi
(ghost fm: format) (fm': format) (nz pz: t) (m: mode) (b: t) : t
2423
requires { widens fm fm' /\ fmt b = fm /\ fmt nz = fm /\ fmt pz = fm }
2424
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
2425
requires { is_not_nan b /\ not is_plus_infinity b }
2426
ensures { fmt result = fm' /\ is_not_nan result }
2427
ensures { forall z e. le_dz (round_f fm m z e) b -> ele e (ext result) }
2428
= let b0 = if is_zero_f fm nz pz b then pz else b in
2429
{proof…}
2430
index_range b;
2431
assert { index b <> nb_sign fm - 1 };
2432
assert { le_dz b b0 /\ is_not_nan b0 /\ fmt b0 = fm };
2433
let ghost j = index b0 in
2434
assert { j <> -1 /\ - nb_sign fm <= j < nb_sign fm - 1 };
2435
convert_widen fm' rtn b0; widen_nb_sign fm fm';
2436
let ghost w0 = windex j in
2437
assert { - nb_sign fm' <= w0 <= nb_sign fm' - 3 };
2438
assert { - nb_sign fm' <= w0 + hi_off m j };
2439
of_index_range fm' w0; of_index_range fm' (w0 + 1); of_index_range fm' (w0 + 2);
2440
{qed}
2441
let w = convert fm' rtn b0 in
2442
let t = match m with
2443
| RTN -> succ_diff_zero (succ_diff_zero w)
2444
| RTP -> w
2445
| RTZ -> if lt_diff_zero b0 pz then w else succ_diff_zero (succ_diff_zero w)
2446
| RNE | RNA -> succ_diff_zero w
2447
end in
2448
{proof…}
2449
assert { t = of_index fm' (w0 + hi_off m j) };
2450
of_index_range fm' (w0 + hi_off m j);
2451
let lemma thr (z: bool) (e: ereal) : unit
2452
requires { le_dz (round_f fm m z e) b } ensures { ele e (ext t) }
2453
= le_trans (round_f fm m z e) b b0 in
2454
{qed}
2455
t

Backward conversion

The x of the format fm whose conversion to fm' is in [a, b]: at or above thr_lo a (in fm' with one more bit) rounded up in fm, at or below thr_hi b rounded down (the rounding is monotonic, and exact on the floats of fm), then at most two steps while the bound itself converts out of [a, b]. The conversion keeps the sign: a +0 a (a –0 b) keeps the positive (negative) x.

  Valid (492ms) (cvc5)
2466
let rec
conv_trim_lo
(ghost fm: format) (fm': format) (pinf: t) (m: mode) (a l: t)
2467
(fuel: int) : t
2468
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
2469
requires { fmt a = fm' /\ fmt l = fm /\ is_not_nan l }
2470
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_convert fm' m x) -> le_dz l x }
2471
ensures { fmt result = fm /\ is_not_nan result }
2472
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_convert fm' m x) -> le_dz result x }
2473
variant { fuel }
2474
= if fuel <= 0 || is_pinf fm pinf l then l
2475
else begin
2476
{proof…} convert_not_nan fm' m l; {qed}
2477
if lt_diff_zero (convert fm' m l) a then begin
2478
{proof…}
2479
let lemma pt (x: t) : unit
2480
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_convert fm' m x) }
2481
ensures { le_dz (succ_dz l) x }
2482
= lt_not_le (fp_convert fm' m l) a;
2483
if pure { x = l } then absurd;
2484
index_range x; index_range l;
2485
assert { lt_dz l x };
2486
succ_least l x in
2487
index_range l; of_index_range fm (index l + 1);
2488
{qed}
2489
conv_trim_lo fm fm' pinf m a (succ_diff_zero l) (fuel - 1)
2490
end else l
2491
end
2492
Valid (130ms) (cvc5)
2493
let rec
conv_trim_hi
(ghost fm: format) (fm': format) (minf: t) (m: mode) (b h: t)
2494
(fuel: int) : t
2495
requires { fmt minf = fm /\ minf = minus_infinity fm }
2496
requires { fmt b = fm' /\ fmt h = fm /\ is_not_nan h }
2497
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_convert fm' m x) b -> le_dz x h }
2498
ensures { fmt result = fm /\ is_not_nan result }
2499
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_convert fm' m x) b -> le_dz x result }
2500
variant { fuel }
2501
= if fuel <= 0 || is_minf fm minf h then h
2502
else begin
2503
{proof…} convert_not_nan fm' m h; {qed}
2504
if lt_diff_zero b (convert fm' m h) then begin
2505
{proof…}
2506
let lemma pt (x: t) : unit
2507
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_convert fm' m x) b }
2508
ensures { le_dz x (pred_dz h) }
2509
= lt_not_le b (fp_convert fm' m h);
2510
if pure { x = h } then absurd;
2511
index_range x; index_range h;
2512
assert { lt_dz x h };
2513
pred_greatest h x in
2514
index_range h; of_index_range fm (index h - 1);
2515
{qed}
2516
conv_trim_hi fm fm' minf m b (pred_diff_zero h) (fuel - 1)
2517
end else h
2518
end
2519
Valid (719ms) (colibri2 76) (cvc4 2) (cvc5 15) (split_vc 3) (depth 2)
2520
let
convert_back_iv
(fm: format) (fm': format) (minf pinf minf' pinf': t) (m: mode)
2521
(a b: t)
2522
: dom
2523
requires { fmt minf = fm /\ fmt pinf = fm }
2524
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
2525
requires { fmt minf' = fm' /\ fmt pinf' = fm' }
2526
requires { minf' = minus_infinity fm' /\ pinf' = plus_infinity fm' }
2527
requires { fmt a = fm' /\ fmt b = fm' /\ le_dz a b }
2528
ensures { result.fm = fm }
2529
ensures { forall x. fmt x = fm -> is_not_nan x ->
2530
in_iv (fp_convert fm' m x) a b -> mem x result }
2531
= {proof…}
2532
le_not_nan a b; format_facts fm (); format_facts fm' ();
2533
of_index_range fm 0; of_index_range fm (-1);
2534
of_index_range fm' 0; of_index_range fm' (-1);
2535
index_range minf; index_range pinf;
2536
{qed}
2537
let nz = of_index_v fm (-1) in
2538
let pz = of_index_v fm 0 in
2539
let nz' = of_index_v fm' (-1) in
2540
let pz' = of_index_v fm' 0 in
2541
match widen fm' with
2542
| None ->
2543
{proof…}
2544
let lemma pt (x: t) : unit
2545
requires { fmt x = fm /\ is_not_nan x } ensures { in_iv x minf pinf }
2546
= index_range x in
2547
{qed}
2548
interval fm minf pinf
2549
| Some fw ->
2550
let lo =
2551
if is_minf fm' minf' a then minf
2552
else if is_pz fm' pz' a then begin
2553
(* the conversion keeps the sign: only the positive `x` reach +0 *)
2554
{proof…}
2555
let lemma pt (x: t) : unit
2556
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_convert fm' m x) }
2557
ensures { le_dz pz x }
2558
= index_range x; index_range nz; index_range pz;
2559
if pure { index x < 0 } then begin
2560
convert_le fm' m x nz;
2561
let ghost cz = fp_convert fm' m nz in
2562
convert_not_nan fm' m nz;
2563
assert { ext nz = Fin 0.0 };
2564
assert { cz = round_f fm' m true (Fin 0.0) };
2565
assert { is_zero cz /\ not is_positive cz };
2566
index_range cz; index_range a;
2567
assert { index cz = -1 };
2568
le_trans a (fp_convert fm' m x) cz;
2569
absurd
2570
end in
2571
{qed}
2572
pz
2573
end else begin
2574
let t = thr_lo fm' fw nz' pz' m a in
2575
let l0 = convert fm rtp t in
2576
{proof…} convert_not_nan fm rtp t; {qed}
2577
let l = if is_zero_f fm nz pz l0 then nz else l0 in
2578
{proof…}
2579
let lemma pt (x: t) : unit
2580
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_convert fm' m x) }
2581
ensures { le_dz l x }
2582
= let ghost zx = if is_positive x then false else true in
2583
assert { fp_convert fm' m x = round_f fm' m zx (ext x) };
2584
assert { ele (ext t) (ext x) };
2585
let ghost zt = if is_positive t then false else true in
2586
assert { l0 = round_f fm rtp zt (ext t) };
2587
round_f_ele fm rtp zt zx (ext t) (ext x);
2588
assert { round_f fm rtp zx (ext x) = x };
2589
assert { ele (ext l) (ext x) };
2590
ele_le l x in
2591
{qed}
2592
conv_trim_lo fm fm' pinf m a l 2
2593
end in
2594
let hi =
2595
if is_pinf fm' pinf' b then pinf
2596
else if is_nz fm' nz' b then begin
2597
(* only the negative `x` reach -0 *)
2598
{proof…}
2599
let lemma pt (x: t) : unit
2600
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_convert fm' m x) b }
2601
ensures { le_dz x nz }
2602
= index_range x; index_range nz; index_range pz;
2603
if pure { 0 <= index x } then begin
2604
convert_le fm' m pz x;
2605
let ghost cz = fp_convert fm' m pz in
2606
convert_not_nan fm' m pz;
2607
assert { ext pz = Fin 0.0 };
2608
assert { cz = round_f fm' m false (Fin 0.0) };
2609
assert { is_zero cz /\ is_positive cz };
2610
index_range cz; index_range b;
2611
assert { index cz = 0 };
2612
le_trans cz (fp_convert fm' m x) b;
2613
absurd
2614
end in
2615
{qed}
2616
nz
2617
end else begin
2618
let t = thr_hi fm' fw nz' pz' m b in
2619
let h0 = convert fm rtn t in
2620
{proof…} convert_not_nan fm rtn t; {qed}
2621
let h = if is_zero_f fm nz pz h0 then pz else h0 in
2622
{proof…}
2623
let lemma pt (x: t) : unit
2624
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_convert fm' m x) b }
2625
ensures { le_dz x h }
2626
= let ghost zx = if is_positive x then false else true in
2627
assert { fp_convert fm' m x = round_f fm' m zx (ext x) };
2628
assert { ele (ext x) (ext t) };
2629
let ghost zt = if is_positive t then false else true in
2630
assert { h0 = round_f fm rtn zt (ext t) };
2631
round_f_ele fm rtn zx zt (ext x) (ext t);
2632
assert { round_f fm rtn zx (ext x) = x };
2633
assert { ele (ext x) (ext h) };
2634
ele_le x h in
2635
{qed}
2636
conv_trim_hi fm fm' minf m b h 2
2637
end in
2638
{proof…}
2639
let lemma pt (x: t) : unit
2640
requires { fmt x = fm /\ is_not_nan x /\ in_iv (fp_convert fm' m x) a b }
2641
ensures { le_dz lo x /\ le_dz x hi }
2642
= index_range x in
2643
{qed}
2644
if lt_diff_zero hi lo then begin
2645
{proof…}
2646
let lemma no (x: t) : unit
2647
requires { fmt x = fm /\ is_not_nan x /\ in_iv (fp_convert fm' m x) a b }
2648
ensures { false }
2649
= pt x; le_trans lo x hi; lt_not_le hi lo in
2650
{qed}
2651
empty_dom fm
2652
end else begin
2653
{proof…} total lo hi; {qed}
2654
interval fm lo hi
2655
end
2656
end
2657
Valid (477ms) (cvc5)
2658
let rec
convert_back_u
(fm: format) (fm': format) (minf pinf minf' pinf': t) (m: mode)
2659
(l: list (t, t)) : dom
2660
requires { fmt minf = fm /\ fmt pinf = fm }
2661
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
2662
requires { fmt minf' = fm' /\ fmt pinf' = fm' }
2663
requires { minf' = minus_infinity fm' /\ pinf' = plus_infinity fm' }
2664
requires { ok_u l /\ fmt_u fm' l }
2665
ensures { result.fm = fm }
2666
ensures { forall x. fmt x = fm -> is_not_nan x ->
2667
mem_u (fp_convert fm' m x) l -> mem x result }
2668
variant { l }
2669
= match l with
2670
| Nil -> empty_dom fm
2671
| Cons (a, b) r ->
2672
union fm (convert_back_iv fm fm' minf pinf minf' pinf' m a b)
2673
(convert_back_u fm fm' minf pinf minf' pinf' m r)
2674
end

The floats of the format fm whose conversion to the format fm' is in r

  Valid (326ms) (cvc5)
2678
let
convert_back
(fm: format) (fm': format) (minf pinf minf' pinf': t) (m: mode)
2679
(r: dom)
2680
: dom
2681
requires { fmt minf = fm /\ fmt pinf = fm }
2682
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
2683
requires { fmt minf' = fm' /\ fmt pinf' = fm' }
2684
requires { minf' = minus_infinity fm' /\ pinf' = plus_infinity fm' }
2685
requires { r.fm = fm' }
2686
ensures { result.fm = fm }
2687
ensures { forall x. fmt x = fm -> mem (fp_convert fm' m x) r -> mem x result }
2688
= let u = convert_back_u fm fm' minf pinf minf' pinf' m r.u in
2689
{proof…}
2690
let lemma pt (x: t) : unit
2691
requires { fmt x = fm /\ is_nan x } ensures { is_nan (fp_convert fm' m x) }
2692
= () in
2693
{qed}
2694
if r.nan then union fm (nan_dom fm) u else u

The reals that round into an interval

to_fp of SMT–LIB from a real r rounds Fin r with a zero result negative when r < 0. The reals that round to a or above are at or above real_lo a (–inf when a is –inf); the ones that round to b or below are at or below real_hi b (+inf when b is +inf), strictly when the flag is true. Unlike thr_lo and thr_hi, the sign of zero is used: the reals that round to +0 or above are at or above 0, the ones that round to –0 or below are below 0.

  Valid (824ms) (cvc4)
2705
let
real_lo
(ghost fm: format) (fm': format) (minf nz pz: t) (m: mode) (a: t) : t
2706
requires { widens fm fm' /\ fmt a = fm /\ fmt minf = fm /\ fmt nz = fm /\ fmt pz = fm }
2707
requires { minf = minus_infinity fm /\ at_index fm nz (-1) /\ at_index fm pz 0 }
2708
requires { is_not_nan a }
2709
ensures { fmt result = fm' /\ is_not_nan result }
2710
ensures { forall r. le_dz a (round_f fm m (r <. 0.0) (Fin r)) -> ele (ext result) (Fin r) }
2711
= if is_minf fm minf a then begin
2712
{proof…}
2713
assert { ext (fp_convert fm' rtn a) = MInf };
2714
{qed}
2715
convert fm' rtn a
2716
end else if is_pz fm pz a then begin
2717
{proof…}
2718
let ghost z = convert fm' rtn pz in
2719
assert { is_zero (round_f fm' rtn false (Fin 0.0)) };
2720
assert { ext z = Fin 0.0 };
2721
let lemma pos (r: real) : unit
2722
requires { le_dz a (round_f fm m (r <. 0.0) (Fin r)) } ensures { 0.0 <=. r }
2723
= if r <. 0.0 then begin
2724
let x = round_f fm m true (Fin r) in
2725
let y = round_f fm m true (Fin 0.0) in
2726
assert { le_dz x y };
2727
assert { is_zero y /\ not is_positive y };
2728
assert { index y = -1 };
2729
assert { index a <= index x <= index y };
2730
absurd
2731
end in
2732
{qed}
2733
convert fm' rtn pz
2734
end else thr_lo fm fm' nz pz m a
2735
Valid (1.6s) (cvc4)
2736
let
real_hi
(ghost fm: format) (fm': format) (pinf nz pz: t) (m: mode) (b: t) : (t, bool)
2737
requires { widens fm fm' /\ fmt b = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
2738
requires { pinf = plus_infinity fm /\ at_index fm nz (-1) /\ at_index fm pz 0 }
2739
requires { is_not_nan b }
2740
ensures { let (h, _) = result in fmt h = fm' /\ is_not_nan h }
2741
ensures { let (h, s) = result in forall r. le_dz (round_f fm m (r <. 0.0) (Fin r)) b ->
2742
if s then Fin r <> ext h /\ ele (Fin r) (ext h) else ele (Fin r) (ext h) }
2743
= if is_pinf fm pinf b then begin
2744
{proof…}
2745
assert { ext (fp_convert fm' rtn b) = PInf };
2746
{qed}
2747
(convert fm' rtn b, false)
2748
end else if is_nz fm nz b then begin
2749
{proof…}
2750
let ghost z = convert fm' rtn pz in
2751
assert { is_zero (round_f fm' rtn false (Fin 0.0)) };
2752
assert { ext z = Fin 0.0 };
2753
let lemma neg (r: real) : unit
2754
requires { le_dz (round_f fm m (r <. 0.0) (Fin r)) b } ensures { r <. 0.0 }
2755
= if not (r <. 0.0) then begin
2756
let x = round_f fm m false (Fin r) in
2757
let y = round_f fm m false (Fin 0.0) in
2758
assert { le_dz y x };
2759
assert { is_zero y /\ is_positive y };
2760
assert { index y = 0 };
2761
assert { index y <= index x <= index b };
2762
absurd
2763
end in
2764
{qed}
2765
(convert fm' rtn pz, true)
2766
end else (thr_hi fm fm' nz pz m b, false)

The least x such that x + d rounds to a or above, d finite: at or above t - d with t = thr_lo a, strictly when t rounds below a, computed with directed rounding in the format fm' then in fm. Sound by monotonicity; exact (tested against the former bisection).

  Valid (651ms) (colibri2 53) (cvc4 2) (cvc5 22) (split_vc 3) (depth 2)
2773
let
add_lo
(ghost fm: format) (fm': format) (nz pz: t) (m: mode) (a d: t) : t
2774
requires { widens fm fm' /\ fmt a = fm /\ fmt d = fm /\ fmt nz = fm /\ fmt pz = fm }
2775
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
2776
requires { is_not_nan a /\ not is_minus_infinity a /\ is_not_nan d /\ not is_infinity d }
2777
ensures { fmt result = fm /\ is_not_nan result }
2778
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_add m x d) -> le_dz result x }
2779
= let fr = format_of a in
2780
let t = thr_lo fm fm' nz pz m a in
2781
let a0 = if is_zero_f fm nz pz a then nz else a in
2782
let d' = convert fm' rtn d in
2783
let c = convert fr m t in
2784
{proof…}
2785
convert_widen fm' rtn d; convert_not_nan fm m t;
2786
let ghost v = match ext d with Fin v -> v | _ -> absurd end in
2787
2788
assert { ext (neg d') = Fin (-. v) /\ fmt (neg d') = fm' };
2789
assert { not is_infinity (neg d') };
2790
assert { le_dz a0 a /\ fmt a0 = fm /\ is_not_nan a0 };
2791
(* the sum, rounded *)
2792
let lemma sum (x: t) : unit
2793
requires { fmt x = fm /\ is_not_nan x }
2794
ensures { fp_add m x d = round_f fm m (add_zneg m x d) (eadd (ext x) (Fin v)) }
2795
= () in
2796
{qed}
2797
if le_diff_zero a0 c then begin
2798
let s = add rtp t (neg d') in
2799
let l = convert fr rtp s in
2800
{proof…} convert_not_nan fm rtp s; {qed}
2801
let r = if is_zero_f fm nz pz l then nz else l in
2802
{proof…}
2803
let lemma pt (x: t) : unit
2804
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_add m x d) }
2805
ensures { le_dz r x }
2806
= sum x;
2807
let ghost e = eadd (ext x) (Fin v) in
2808
assert { ele (ext t) e };
2809
eadd_move_ge (ext t) (ext x) v;
2810
let ghost zx = if is_positive x then false else true in
2811
convert_widen fm' rtp x;
2812
assert { fp_convert fm' rtp x = round_f fm' rtp zx (ext x) };
2813
round_f_ele fm' rtp (add_zneg rtp t (neg d')) zx (eadd (ext t) (Fin (-. v))) (ext x);
2814
assert { ele (ext s) (ext x) };
2815
let ghost zs = if is_positive s then false else true in
2816
round_f_ele fm rtp zs zx (ext s) (ext x);
2817
assert { round_f fm rtp zx (ext x) = x };
2818
assert { ele (ext l) (ext x) };
2819
assert { ele (ext r) (ext x) };
2820
ele_le r x in
2821
{qed}
2822
r
2823
end else begin
2824
let s = add rtn t (neg d') in
2825
let l = convert fr rtn s in
2826
{proof…} convert_not_nan fm rtn s; {qed}
2827
{proof…}
2828
let lemma below (x: t) : unit
2829
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_add m x d) }
2830
ensures { lt_dz l x /\ not ele (ext x) (ext l) }
2831
= sum x;
2832
let ghost e = eadd (ext x) (Fin v) in
2833
let ghost zt = if is_positive t then false else true in
2834
assert { ele (ext t) e };
2835
(* `t` itself rounds below `a0`, so `e <> ext t` *)
2836
total a0 c;
2837
assert { lt_dz c a0 };
2838
assert { not (is_zero c /\ is_zero a0) by is_zero a0 -> a0 = nz };
2839
assert { not ele (ext a0) (ext c) };
2840
if pure { e = ext t } then begin
2841
round_f_val fm m (add_zneg m x d) zt e;
2842
ext_le a (fp_add m x d); ext_le a0 a;
2843
absurd
2844
end;
2845
eadd_move_gt (ext t) (ext x) v;
2846
round_f_down fm' (add_zneg rtn t (neg d')) (eadd (ext t) (Fin (-. v)));
2847
let ghost zs = if is_positive s then false else true in
2848
round_f_down fm zs (ext s);
2849
assert { not ele (ext x) (ext l) };
2850
total l x;
2851
if pure { le_dz x l } then begin ext_le x l; absurd end in
2852
{qed}
2853
{proof…}
2854
total a0 c;
2855
if pure { is_plus_infinity t } then begin
2856
assert { ext t = PInf /\ c = plus_infinity fm };
2857
index_range a0; index_range c; assert { le_dz a0 c }; absurd
2858
end;
2859
round_f_down fm' (add_zneg rtn t (neg d')) (eadd (ext t) (Fin (-. v)));
2860
let ghost zs0 = if is_positive s then false else true in
2861
round_f_down fm zs0 (ext s);
2862
assert { ext s <> PInf /\ ext l <> PInf };
2863
assert { not is_plus_infinity l };
2864
{qed}
2865
if is_zero_f fm nz pz l then begin
2866
{proof…}
2867
let lemma pt (x: t) : unit
2868
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_add m x d) }
2869
ensures { le_dz (succ_dz pz) x }
2870
= below x;
2871
assert { ext l = Fin 0.0 /\ ext pz = Fin 0.0 };
2872
total pz x;
2873
if pure { le_dz x pz } then begin ext_le x pz; ext_le l x; absurd end;
2874
succ_least pz x in
2875
format_facts fm (); of_index_range fm 1;
2876
{qed}
2877
succ_diff_zero pz
2878
end else begin
2879
{proof…}
2880
let lemma pt (x: t) : unit
2881
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_add m x d) }
2882
ensures { le_dz (succ_dz l) x }
2883
= below x; succ_least l x in
2884
index_range l; of_index_range fm (index l + 1);
2885
{qed}
2886
succ_diff_zero l
2887
end
2888
end

The greatest x such that x + c rounds to b or below, c finite, symmetric to add_lo

  Valid (681ms) (colibri2 60) (cvc4 8) (cvc5 9) (split_vc 3) (depth 2)
2892
let
add_hi
(ghost fm: format) (fm': format) (nz pz: t) (m: mode) (b c: t) : t
2893
requires { widens fm fm' /\ fmt b = fm /\ fmt c = fm /\ fmt nz = fm /\ fmt pz = fm }
2894
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
2895
requires { is_not_nan b /\ not is_plus_infinity b /\ is_not_nan c /\ not is_infinity c }
2896
ensures { fmt result = fm /\ is_not_nan result }
2897
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_add m x c) b -> le_dz x result }
2898
= let fr = format_of b in
2899
let t = thr_hi fm fm' nz pz m b in
2900
let b0 = if is_zero_f fm nz pz b then pz else b in
2901
let c' = convert fm' rtn c in
2902
let k = convert fr m t in
2903
{proof…}
2904
convert_widen fm' rtn c; convert_not_nan fm m t;
2905
let ghost v = match ext c with Fin v -> v | _ -> absurd end in
2906
2907
assert { ext (neg c') = Fin (-. v) /\ fmt (neg c') = fm' };
2908
assert { not is_infinity (neg c') };
2909
assert { le_dz b b0 /\ fmt b0 = fm /\ is_not_nan b0 };
2910
let lemma sum (x: t) : unit
2911
requires { fmt x = fm /\ is_not_nan x }
2912
ensures { fp_add m x c = round_f fm m (add_zneg m x c) (eadd (ext x) (Fin v)) }
2913
= () in
2914
{qed}
2915
if le_diff_zero k b0 then begin
2916
let s = add rtn t (neg c') in
2917
let h = convert fr rtn s in
2918
{proof…} convert_not_nan fm rtn s; {qed}
2919
let r = if is_zero_f fm nz pz h then pz else h in
2920
{proof…}
2921
let lemma pt (x: t) : unit
2922
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_add m x c) b }
2923
ensures { le_dz x r }
2924
= sum x;
2925
let ghost e = eadd (ext x) (Fin v) in
2926
assert { ele e (ext t) };
2927
eadd_move_le (ext t) (ext x) v;
2928
let ghost zx = if is_positive x then false else true in
2929
convert_widen fm' rtn x;
2930
assert { fp_convert fm' rtn x = round_f fm' rtn zx (ext x) };
2931
round_f_ele fm' rtn zx (add_zneg rtn t (neg c')) (ext x) (eadd (ext t) (Fin (-. v)));
2932
assert { ele (ext x) (ext s) };
2933
let ghost zs = if is_positive s then false else true in
2934
round_f_ele fm rtn zx zs (ext x) (ext s);
2935
assert { round_f fm rtn zx (ext x) = x };
2936
assert { ele (ext x) (ext h) };
2937
assert { ele (ext x) (ext r) };
2938
ele_le x r in
2939
{qed}
2940
r
2941
end else begin
2942
let s = add rtp t (neg c') in
2943
let h = convert fr rtp s in
2944
{proof…} convert_not_nan fm rtp s; {qed}
2945
{proof…}
2946
let lemma above (x: t) : unit
2947
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_add m x c) b }
2948
ensures { lt_dz x h /\ not ele (ext h) (ext x) }
2949
= sum x;
2950
let ghost e = eadd (ext x) (Fin v) in
2951
let ghost zt = if is_positive t then false else true in
2952
assert { ele e (ext t) };
2953
(* `t` itself rounds above `b0`, so `e <> ext t` *)
2954
total k b0;
2955
assert { lt_dz b0 k };
2956
assert { not (is_zero b0 /\ is_zero k) by is_zero b0 -> b0 = pz };
2957
assert { not ele (ext k) (ext b0) };
2958
if pure { e = ext t } then begin
2959
round_f_val fm m (add_zneg m x c) zt e;
2960
ext_le (fp_add m x c) b; ext_le b b0;
2961
absurd
2962
end;
2963
eadd_move_lt (ext t) (ext x) v;
2964
round_f_up fm' (add_zneg rtp t (neg c')) (eadd (ext t) (Fin (-. v)));
2965
let ghost zs = if is_positive s then false else true in
2966
round_f_up fm zs (ext s);
2967
assert { not ele (ext h) (ext x) };
2968
total x h;
2969
if pure { le_dz h x } then begin ext_le h x; absurd end in
2970
total k b0;
2971
if pure { is_minus_infinity t } then begin
2972
assert { ext t = MInf /\ k = minus_infinity fm };
2973
index_range b0; index_range k; assert { le_dz k b0 }; absurd
2974
end;
2975
round_f_up fm' (add_zneg rtp t (neg c')) (eadd (ext t) (Fin (-. v)));
2976
let ghost zs0 = if is_positive s then false else true in
2977
round_f_up fm zs0 (ext s);
2978
assert { ext s <> MInf /\ ext h <> MInf };
2979
assert { not is_minus_infinity h };
2980
{qed}
2981
if is_zero_f fm nz pz h then begin
2982
{proof…}
2983
let lemma pt (x: t) : unit
2984
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_add m x c) b }
2985
ensures { le_dz x (pred_dz nz) }
2986
= above x;
2987
assert { ext h = Fin 0.0 /\ ext nz = Fin 0.0 };
2988
total x nz;
2989
if pure { le_dz nz x } then begin ext_le nz x; ext_le x h; absurd end;
2990
pred_greatest nz x in
2991
format_facts fm (); of_index_range fm (-2);
2992
{qed}
2993
pred_diff_zero nz
2994
end else begin
2995
{proof…}
2996
let lemma pt (x: t) : unit
2997
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_add m x c) b }
2998
ensures { le_dz x (pred_dz h) }
2999
= above x; pred_greatest h x in
3000
index_range h; of_index_range fm (index h - 1);
3001
{qed}
3002
pred_diff_zero h
3003
end
3004
end

One step up when the bound l is not a solution: for a zero a, add_lo takes a as –0, and l can be the x = -d whose exact sum, a zero, has the wrong sign

  Valid (651ms) (cvc5)
3009
let
trim_lo
(ghost fm: format) (pinf: t) (m: mode) (a d l: t) : t
3010
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
3011
requires { fmt a = fm /\ fmt d = fm /\ fmt l = fm /\ is_not_nan l }
3012
requires { is_not_nan d /\ not is_infinity d }
3013
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_add m x d) -> le_dz l x }
3014
ensures { fmt result = fm /\ is_not_nan result }
3015
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_add m x d) -> le_dz result x }
3016
= if is_pinf fm pinf l then l
3017
else if lt_diff_zero (add m l d) a then begin
3018
{proof…}
3019
let lemma pt (x: t) : unit
3020
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_add m x d) }
3021
ensures { le_dz (succ_dz l) x }
3022
= lt_not_le (fp_add m l d) a;
3023
if pure { x = l } then absurd;
3024
index_range x; index_range l;
3025
assert { lt_dz l x };
3026
succ_least l x in
3027
index_range l; of_index_range fm (index l + 1);
3028
{qed}
3029
succ_diff_zero l
3030
end else l
3031
Valid (739ms) (cvc4)
3032
let
trim_hi
(ghost fm: format) (minf: t) (m: mode) (b c h: t) : t
3033
requires { fmt minf = fm /\ minf = minus_infinity fm }
3034
requires { fmt b = fm /\ fmt c = fm /\ fmt h = fm /\ is_not_nan h }
3035
requires { is_not_nan c /\ not is_infinity c }
3036
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_add m x c) b -> le_dz x h }
3037
ensures { fmt result = fm /\ is_not_nan result }
3038
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_add m x c) b -> le_dz x result }
3039
= if is_minf fm minf h then h
3040
else if lt_diff_zero b (add m h c) then begin
3041
{proof…}
3042
let lemma pt (x: t) : unit
3043
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_add m x c) b }
3044
ensures { le_dz x (pred_dz h) }
3045
= lt_not_le b (fp_add m h c);
3046
if pure { x = h } then absurd;
3047
index_range x; index_range h;
3048
assert { lt_dz x h };
3049
pred_greatest h x in
3050
index_range h; of_index_range fm (index h - 1);
3051
{qed}
3052
pred_diff_zero h
3053
end else h

The least z of [lo, hi] with a <= z + w, w finite, or a lower bound of them (add_lo, then two steps of trim_lo, for the zero ends); None when there is none

  Valid (186ms) (cvc5)
3058
let
add_least
(ghost fm: format) (minf pinf: t) (m: mode) (lo hi a w: t) : option t
3059
requires { fmt minf = fm /\ fmt pinf = fm }
3060
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3061
requires { fmt lo = fm /\ fmt hi = fm /\ le_dz lo hi }
3062
requires { fmt a = fm /\ is_not_nan a /\ fmt w = fm /\ is_not_nan w /\ not is_infinity w }
3063
ensures { match result with
3064
| None -> forall z. in_iv z lo hi -> not le_dz a (fp_add m z w)
3065
| Some r -> in_iv r lo hi /\
3066
forall z. in_iv z lo hi -> le_dz a (fp_add m z w) -> le_dz r z end }
3067
= {proof…} le_not_nan lo hi; le_refl lo; le_refl hi; add_special m hi w; {qed}
3068
if lt_diff_zero (add m hi w) a then begin
3069
{proof…}
3070
let lemma no (z: t) : unit
3071
requires { in_iv z lo hi /\ le_dz a (fp_add m z w) } ensures { false }
3072
= le_not_nan lo z; add_special m z w; le_refl w; add_box m z hi w w;
3073
le_trans a (fp_add m z w) (fp_add m hi w); lt_not_le (fp_add m hi w) a in
3074
{qed}
3075
None
3076
end else if is_minf fm minf a then Some lo
3077
else begin
3078
let fr = format_of a in
3079
match widen fr with
3080
| None -> Some lo
3081
| Some fw ->
3082
{proof…} format_facts fm (); of_index_range fm 0; of_index_range fm (-1); {qed}
3083
let nz = of_index_v fr (-1) in
3084
let pz = of_index_v fr 0 in
3085
let l = trim_lo fm pinf m a w (trim_lo fm pinf m a w (add_lo fm fw nz pz m a w)) in
3086
let r = max_diff_zero l lo in
3087
if lt_diff_zero hi r then begin
3088
{proof…}
3089
let lemma no (z: t) : unit
3090
requires { in_iv z lo hi /\ le_dz a (fp_add m z w) } ensures { false }
3091
= le_not_nan lo z; le_trans r z hi; lt_not_le hi r in
3092
{qed}
3093
None
3094
end else begin
3095
{proof…} total r hi; {qed}
3096
Some r
3097
end
3098
end
3099
end

The greatest z of [lo, hi] with z + w <= b, w finite, or an upper bound of them (add_hi, then two steps of trim_hi); None when there is none

  Valid (181ms) (cvc5)
3104
let
add_greatest
(ghost fm: format) (minf pinf: t) (m: mode) (lo hi b w: t) : option t
3105
requires { fmt minf = fm /\ fmt pinf = fm }
3106
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3107
requires { fmt lo = fm /\ fmt hi = fm /\ le_dz lo hi }
3108
requires { fmt b = fm /\ is_not_nan b /\ fmt w = fm /\ is_not_nan w /\ not is_infinity w }
3109
ensures { match result with
3110
| None -> forall z. in_iv z lo hi -> not le_dz (fp_add m z w) b
3111
| Some r -> in_iv r lo hi /\
3112
forall z. in_iv z lo hi -> le_dz (fp_add m z w) b -> le_dz z r end }
3113
= {proof…} le_not_nan lo hi; le_refl lo; le_refl hi; add_special m lo w; {qed}
3114
if lt_diff_zero b (add m lo w) then begin
3115
{proof…}
3116
let lemma no (z: t) : unit
3117
requires { in_iv z lo hi /\ le_dz (fp_add m z w) b } ensures { false }
3118
= le_not_nan lo z; add_special m z w; le_refl w; add_box m lo z w w;
3119
le_trans (fp_add m lo w) (fp_add m z w) b; lt_not_le b (fp_add m lo w) in
3120
{qed}
3121
None
3122
end else if is_pinf fm pinf b then Some hi
3123
else begin
3124
let fr = format_of b in
3125
match widen fr with
3126
| None -> Some hi
3127
| Some fw ->
3128
{proof…} format_facts fm (); of_index_range fm 0; of_index_range fm (-1); {qed}
3129
let nz = of_index_v fr (-1) in
3130
let pz = of_index_v fr 0 in
3131
let h = trim_hi fm minf m b w (trim_hi fm minf m b w (add_hi fm fw nz pz m b w)) in
3132
let r = min_diff_zero h hi in
3133
if lt_diff_zero r lo then begin
3134
{proof…}
3135
let lemma no (z: t) : unit
3136
requires { in_iv z lo hi /\ le_dz (fp_add m z w) b } ensures { false }
3137
= le_not_nan lo z; le_trans lo z r; lt_not_le r lo in
3138
{qed}
3139
None
3140
end else begin
3141
{proof…} total lo r; {qed}
3142
Some r
3143
end
3144
end
3145
end

The x such that x + y is in [a, b] for some y in [c, d], by add_lo and add_hi

  Valid (424ms) (colibri2 58) (cvc4 7) (cvc5 13) (split_vc) (depth 1)
3149
let
add_back_iv_d
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode)
3150
(a b c d: t) : dom
3151
requires { widens fm fm' }
3152
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3153
requires { specials4 fm minf pinf nz pz }
3154
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
3155
requires { le_dz a b /\ le_dz c d }
3156
ensures { result.fm = fm }
3157
ensures { forall x y. fmt x = fm -> is_not_nan x -> in_iv y c d ->
3158
in_iv (fp_add m x y) a b -> mem x result }
3159
= {proof…}
3160
le_not_nan a b; le_not_nan c d;
3161
{qed}
3162
let lo =
3163
if is_minf fm minf a || is_pinf fm pinf d then Some minf
3164
else if is_minf fm minf d then None
3165
else Some (trim_lo fm pinf m a d (add_lo fm fm' nz pz m a d)) in
3166
let hi =
3167
if is_pinf fm pinf b || is_minf fm minf c then Some pinf
3168
else if is_pinf fm pinf c then None
3169
else Some (trim_hi fm minf m b c (add_hi fm fm' nz pz m b c)) in
3170
match lo, hi with
3171
| Some lo, Some hi ->
3172
{proof…}
3173
let lemma pt (x y: t) : unit
3174
requires { fmt x = fm /\ is_not_nan x /\ in_iv y c d /\ in_iv (fp_add m x y) a b }
3175
ensures { le_dz lo x /\ le_dz x hi }
3176
= le_not_nan c y; le_not_nan y d; le_not_nan a (fp_add m x y); le_refl x;
3177
if pure { is_minus_infinity a \/ is_plus_infinity d } then begin
3178
if pure { is_minus_infinity x } then begin
3179
minus_infinity_unique x; le_refl x
3180
end else begin
3181
minus_infinity_lt x; lt_le minf x
3182
end
3183
end else begin
3184
add_special m x d; add_box m x x y d;
3185
le_trans a (fp_add m x y) (fp_add m x d)
3186
end;
3187
if pure { is_plus_infinity b \/ is_minus_infinity c } then begin
3188
if pure { is_plus_infinity x } then begin
3189
plus_infinity_unique x; le_refl x
3190
end else begin
3191
lt_plus_infinity x; lt_le x pinf
3192
end
3193
end else begin
3194
add_special m x c; add_box m x x c y;
3195
le_trans (fp_add m x c) (fp_add m x y) b
3196
end in
3197
{qed}
3198
if lt_diff_zero hi lo then begin
3199
{proof…}
3200
let lemma no (x y: t) : unit
3201
requires { fmt x = fm /\ is_not_nan x /\ in_iv y c d /\ in_iv (fp_add m x y) a b }
3202
ensures { false }
3203
= pt x y; le_trans lo x hi; lt_not_le hi lo in
3204
{qed}
3205
empty_dom fm
3206
end else begin
3207
{proof…} total lo hi; {qed}
3208
interval fm lo hi
3209
end
3210
| _ ->
3211
{proof…}
3212
let lemma no (x y: t) : unit
3213
requires { fmt x = fm /\ is_not_nan x /\ in_iv y c d /\ in_iv (fp_add m x y) a b }
3214
ensures { false }
3215
= le_not_nan c y; le_not_nan a (fp_add m x y);
3216
if pure { is_minus_infinity d } then begin
3217
index_range y; index_range d;
3218
assert { is_minus_infinity y by y = d \/ lt_dz y d };
3219
if pure { is_plus_infinity x } then add_special m x y
3220
else begin
3221
add_minus_infinity m x y;
3222
minus_infinity_unique a
3223
end
3224
end else begin
3225
index_range y; index_range c;
3226
assert { is_plus_infinity y by y = c \/ lt_dz c y };
3227
if pure { is_minus_infinity x } then add_special m x y
3228
else begin
3229
add_plus_infinity m x y;
3230
plus_infinity_unique b
3231
end
3232
end in
3233
{qed}
3234
empty_dom fm
3235
end

The direct bounds when the format with one more bit exists (always, in fbin), else every float

  Valid (491ms) (colibri2 11) (cvc4) (cvc5) (split_vc) (depth 1)
3239
let
add_back_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b c d: t) : dom
3240
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3241
requires { specials4 fm minf pinf nz pz }
3242
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
3243
requires { le_dz a b /\ le_dz c d }
3244
ensures { result.fm = fm }
3245
ensures { forall x y. fmt x = fm -> is_not_nan x -> in_iv y c d ->
3246
in_iv (fp_add m x y) a b -> mem x result }
3247
= match widen (format_of a) with
3248
| Some fm' -> add_back_iv_d fm fm' minf pinf nz pz m a b c d
3249
| None ->
3250
{proof…}
3251
index_range minf; index_range pinf;
3252
let lemma pt (x: t) : unit
3253
requires { fmt x = fm /\ is_not_nan x } ensures { in_iv x minf pinf }
3254
= index_range x in
3255
{qed}
3256
interval fm minf pinf
3257
end
3258
Valid (494ms) (cvc5)
3259
let rec
add_back_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b: t)
3260
(l: list (t, t)) : dom
3261
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3262
requires { specials4 fm minf pinf nz pz }
3263
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l /\ fmt_u fm l }
3264
ensures { result.fm = fm }
3265
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l ->
3266
in_iv (fp_add m x y) a b -> mem x result }
3267
variant { l }
3268
= match l with
3269
| Nil -> empty_dom fm
3270
| Cons (c, d) r ->
3271
union fm (add_back_iv fm minf pinf nz pz m a b c d)
3272
(add_back_iv_u fm minf pinf nz pz m a b r)
3273
end
3274
Valid (348ms) (colibri2)
3275
let rec
add_back_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
3276
(l1 l2: list (t, t)) : dom
3277
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3278
requires { specials4 fm minf pinf nz pz }
3279
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
3280
ensures { result.fm = fm }
3281
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l2 ->
3282
mem_u (fp_add m x y) l1 -> mem x result }
3283
variant { l1 }
3284
= match l1 with
3285
| Nil -> empty_dom fm
3286
| Cons (a, b) r ->
3287
union fm (add_back_iv_u fm minf pinf nz pz m a b l2)
3288
(add_back_u fm minf pinf nz pz m r l2)
3289
end

The x such that x + y is in r for some y in d; by commutativity (Facts.add_comm), also the y such that x + y is in r for some x in d

  Valid (701ms) (cvc5)
3294
let
add_back
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r d: dom) : dom
3295
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3296
requires { specials4 fm minf pinf nz pz }
3297
requires { r.fm = fm /\ d.fm = fm }
3298
ensures { result.fm = fm }
3299
ensures { forall x y. fmt x = fm -> mem y d -> mem (fp_add m x y) r -> mem x result }
3300
ensures { forall x y. fmt y = fm -> mem x d -> mem (fp_add m x y) r -> mem y result }
3301
= let u = add_back_u fm minf pinf nz pz m r.u d.u in
3302
let n =
3303
if r.nan then
3304
if d.nan then top fm minf pinf
3305
else begin
3306
let p = if meets fm minf minf d then singleton fm pinf else empty_dom fm in
3307
let q = if meets fm pinf pinf d then singleton fm minf else empty_dom fm in
3308
union fm (nan_dom fm) (union fm p q)
3309
end
3310
else empty_dom fm in
3311
let res = union fm n u in
3312
{proof…}
3313
let lemma pt (x y: t) : unit
3314
requires { fmt x = fm /\ mem y d /\ mem (fp_add m x y) r }
3315
ensures { mem x res }
3316
= add_special m x y;
3317
if pure { is_nan (fp_add m x y) } then begin
3318
if pure { is_not_nan x /\ is_not_nan y } then begin
3319
if pure { is_plus_infinity x } then begin
3320
plus_infinity_unique x; minus_infinity_unique y;
3321
index_range y; assert { index minf <= index y <= index minf }
3322
end else begin
3323
minus_infinity_unique x; plus_infinity_unique y;
3324
index_range y; assert { index pinf <= index y <= index pinf }
3325
end
3326
end
3327
end in
3328
let lemma pt2 (x y: t) : unit
3329
requires { fmt y = fm /\ mem x d /\ mem (fp_add m x y) r }
3330
ensures { mem y res }
3331
= add_comm m x y; pt y x in
3332
{qed}
3333
res

add_back without the infinities of d when r has neither NaN nor an infinity: a finite sum has finite arguments

  Valid (1.3s) (colibri2 28) (cvc4 2) (cvc5 5) (split_vc 3) (depth 3)
3337
let
add_back_fin
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r d: dom) : dom
3338
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
3339
requires { specials4 fm minf pinf nz pz }
3340
requires { r.fm = fm /\ d.fm = fm }
3341
ensures { result.fm = fm }
3342
ensures { forall x y. fmt x = fm -> mem y d -> mem (fp_add m x y) r -> mem x result }
3343
ensures { forall x y. fmt y = fm -> mem x d -> mem (fp_add m x y) r -> mem y result }
3344
= {proof…} index_range minf; index_range pinf; {qed}
3345
if r.nan || meets fm minf minf r || meets fm pinf pinf r then
3346
add_back fm minf pinf nz pz m r d
3347
else begin
3348
{proof…}
3349
format_facts fm (); succ_gt minf; pred_lt pinf;
3350
of_index_range fm (index minf + 1); of_index_range fm (index pinf - 1);
3351
{qed}
3352
let fin = finites fm (succ_diff_zero minf) (pred_diff_zero pinf) in
3353
{proof…}
3354
let lemma fin_arg (x y: t) : unit
3355
requires { fmt x = fm /\ fmt y = fm /\ mem (fp_add m x y) r }
3356
ensures { mem x fin /\ mem y fin }
3357
= add_special m x y; index_range (fp_add m x y);
3358
assert { is_not_nan (fp_add m x y) };
3359
if pure { is_inf x \/ is_inf y } then begin
3360
assert { is_inf (fp_add m x y) };
3361
if pure { is_plus_infinity (fp_add m x y) } then begin
3362
plus_infinity_unique (fp_add m x y);
3363
assert { index pinf <= index (fp_add m x y) <= index pinf }
3364
end else begin
3365
minus_infinity_unique (fp_add m x y);
3366
assert { index minf <= index (fp_add m x y) <= index minf }
3367
end;
3368
absurd
3369
end in
3370
{qed}
3371
match inter fm d fin with
3372
| None -> empty_dom fm
3373
| Some d' -> add_back fm minf pinf nz pz m r d'
3374
end
3375
end

Shaving the ends of the backward addition

Soundness only, after Gallois–Wong, Boldo and Cuoq, "Optimal inverse projection of floating–point addition" (2020). From an end x of an interval of the candidates, a jump skips floats without a y that puts x' + y in the result, a bounded number of times. The bounds of fbin.AddBound (their Lemmas 4 and 5) come first. For one x, y0, the least y with a <= x + y, is searched from the hint a - x (their Theorem 2: in RNE the answer is next to it); x has a solution iff x + y0 <= b. Otherwise every y is at most yd = pred y0, with x' + yd < a, or at least y0, with x' + y0 > b for x' >= x: no x' below the least x1 with a <= x1 + yd has a solution (the soundness half of their Lemma 8, true in every mode and for any set of y). In their setting x1 is the least solution (their Theorem 3).

No y of [c, d] puts x + y in [a, b]

3394
predicate
no_cd
(m: mode) (x a b c d: t) =
3395
forall y. in_iv y c d -> not in_iv (fp_add m x y) a b

No y of l2 puts x + y in [a, b]

3398
predicate
no_y
(m: mode) (x a b: t) (l2: list (t, t)) =
3399
forall y. mem_u y l2 -> not in_iv (fp_add m x y) a b

No y of l2 puts x + y in l1

3402
predicate
no_add
(m: mode) (x: t) (l1 l2: list (t, t)) =
3403
forall y. mem_u y l2 -> not mem_u (fp_add m x y) l1

y -> x + y on [c, d], x finite: not NaN, and commutative

  Valid (270ms) (cvc5)
3406
let ghost
add_mono_x
(fm: format) (m: mode) (x c d: t) : unit
3407
requires { fmt x = fm /\ fmt c = fm /\ is_not_nan x /\ not is_infinity x }
3408
ensures { forall y. in_iv y c d -> fp_add m y x = fp_add m x y /\
3409
is_not_nan (fp_add m x y) }
3410
= {proof…}
3411
let lemma comm (y: t) : unit
3412
requires { in_iv y c d }
3413
ensures { fp_add m y x = fp_add m x y }
3414
ensures { is_not_nan (fp_add m x y) /\ fmt (fp_add m x y) = fm }
3415
= le_not_nan c y; add_comm m x y; add_special m x y in
3416
let lemma mo (y1 y2: t) : unit
3417
requires { in_iv y1 c d /\ in_iv y2 c d /\ le_dz y1 y2 }
3418
ensures { le_dz (fp_add m y1 x) (fp_add m y2 x) }
3419
= comm y1; comm y2; le_refl x; add_box m x x y1 y2 in
3420
() {qed}

One float: [x, succ x) is {x}

  Valid (449ms) (cvc5)
3423
let
step_lo
(x h: t) : option t
3424
requires { is_not_nan x /\ le_dz x h }
3425
ensures { match result with
3426
| None -> forall x'. in_iv x' x h -> x' = x
3427
| Some x1 -> in_iv x1 x h /\ forall x'. le_dz x x' -> lt_dz x' x1 -> x' = x
3428
end }
3429
= if le_diff_zero h x then begin
3430
{proof…}
3431
let lemma pt (x': t) : unit
3432
requires { in_iv x' x h } ensures { x' = x }
3433
= le_trans x' h x in
3434
{qed}
3435
None
3436
end else begin
3437
{proof…}
3438
le_not_nan x h; total h x; lt_not_plus_infinity x h; succ_gt x; succ_least x h;
3439
let lemma first (x': t) : unit
3440
requires { le_dz x x' /\ lt_dz x' (succ_dz x) } ensures { x' = x }
3441
= total x x';
3442
if pure { lt_dz x x' } then begin
3443
succ_least x x'; lt_not_le x' (succ_dz x); absurd
3444
end in
3445
{qed}
3446
Some (succ_diff_zero x)
3447
end

One float: (pred x, x] is {x}

  Valid (599ms) (cvc4)
3450
let
step_hi
(l x: t) : option t
3451
requires { is_not_nan x /\ le_dz l x }
3452
ensures { match result with
3453
| None -> forall x'. in_iv x' l x -> x' = x
3454
| Some x1 -> in_iv x1 l x /\ forall x'. lt_dz x1 x' -> le_dz x' x -> x' = x
3455
end }
3456
= if le_diff_zero x l then begin
3457
{proof…}
3458
let lemma pt (x': t) : unit
3459
requires { in_iv x' l x } ensures { x' = x }
3460
= le_trans x l x' in
3461
{qed}
3462
None
3463
end else begin
3464
{proof…}
3465
le_not_nan l x; total x l; lt_not_minus_infinity l x; pred_lt x; pred_greatest x l;
3466
let lemma last (x': t) : unit
3467
requires { lt_dz (pred_dz x) x' /\ le_dz x' x } ensures { x' = x }
3468
= total x' x;
3469
if pure { lt_dz x' x } then begin
3470
pred_greatest x x'; lt_not_le (pred_dz x) x'; absurd
3471
end in
3472
{qed}
3473
Some (pred_diff_zero x)
3474
end

A jump up from x in [x, h] for y in [c, d] and sums in [a, b]: the floats of [x, result) have no solution; None when no float of [x, h] has one

  Valid (594ms) (colibri2 115) (cvc4 4) (cvc5 14) (split_vc 2) (depth 2)
3479
let
jump_lo_iv
(ghost fm: format) (minf pinf: t) (m: mode) (x h a b c d: t)
3480
: option t
3481
requires { fmt minf = fm /\ fmt pinf = fm }
3482
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3483
requires { fmt x = fm /\ fmt h = fm /\ le_dz x h }
3484
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
3485
requires { le_dz a b /\ le_dz c d }
3486
ensures { match result with
3487
| None -> forall x'. in_iv x' x h -> no_cd m x' a b c d
3488
| Some x1 -> in_iv x1 x h /\
3489
forall x'. le_dz x x' -> lt_dz x' x1 -> no_cd m x' a b c d
3490
end }
3491
= {proof…} le_not_nan x h; le_not_nan a b; le_not_nan c d; le_refl x; {qed}
3492
if is_inf_f fm minf pinf x then begin
3493
if le_diff_zero a x && le_diff_zero x b then Some x
3494
else begin
3495
{proof…}
3496
let lemma inf (y: t) : unit
3497
requires { in_iv y c d /\ in_iv (fp_add m x y) a b }
3498
ensures { false }
3499
= le_not_nan c y; add_special m x y;
3500
if pure { is_plus_infinity x } then begin
3501
plus_infinity_unique x; plus_infinity_unique (fp_add m x y);
3502
assert { fp_add m x y = x }
3503
end else begin
3504
minus_infinity_unique x; minus_infinity_unique (fp_add m x y);
3505
assert { fp_add m x y = x }
3506
end in
3507
{qed}
3508
step_lo x h
3509
end
3510
end else begin
3511
{proof…} add_mono_x fm m x c d; {qed}
3512
match add_least fm minf pinf m c d a x with
3513
| None ->
3515
(* x + d < a: no x' has a solution below the least x' with a <= x' + d *)
3516
if is_inf_f fm minf pinf d then begin
3517
{proof…}
3518
let lemma no (y: t) : unit
3519
requires { in_iv y c d /\ in_iv (fp_add m x y) a b } ensures { false }
3520
= le_not_nan a (fp_add m x y) in
3521
{qed}
3522
step_lo x h
3523
end else begin
3524
{proof…} le_refl d;
3525
let lemma below (x' y: t) : unit
3526
requires { le_dz x x' /\ lt_dz (fp_add m x' d) a }
3527
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3528
ensures { false }
3529
= le_not_nan a (fp_add m x' y); le_not_nan x x'; add_special m x' d;
3530
le_refl x'; add_box m x' x' y d;
3531
le_lt_trans a (fp_add m x' y) (fp_add m x' d); lt_not_le a a in
3532
{qed}
3533
let r = add_least fm minf pinf m x h a d in
3534
{proof…}
3535
(match r with
3536
| None ->
3537
let lemma pt (x' y: t) : unit
3538
requires { in_iv x' x h /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3539
ensures { false }
3540
= le_not_nan x x'; add_special m x' d; total a (fp_add m x' d); below x' y in
3541
()
3542
| Some x1 ->
3543
let lemma pt (x' y: t) : unit
3544
requires { le_dz x x' /\ lt_dz x' x1 }
3545
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3546
ensures { false }
3547
= le_lt_trans x x' x1; lt_le x' x1; le_trans x' x1 h;
3548
le_not_nan x x'; add_special m x' d; total a (fp_add m x' d);
3549
if pure { le_dz a (fp_add m x' d) } then lt_not_le x' x1;
3550
below x' y in
3551
()
3552
end);
3553
{qed}
3554
r
3555
end
3556
| Some y0 ->
3557
let s = add m x y0 in
3558
if le_diff_zero s b then Some x
3559
else begin
3560
{proof…}
3561
le_not_nan c y0; total s b;
3562
let lemma above (x' y: t) : unit
3563
requires { le_dz x x' /\ le_dz y0 y }
3564
requires { in_iv (fp_add m x' y) a b }
3565
ensures { false }
3566
= le_not_nan a (fp_add m x' y);
3567
add_box m x x' y0 y; lt_le_trans b s (fp_add m x' y);
3568
lt_not_le b (fp_add m x' y) in
3569
{qed}
3570
if le_diff_zero y0 c then begin
3571
{proof…}
3572
let lemma pt (x' y: t) : unit
3573
requires { in_iv x' x h /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3574
ensures { false }
3575
= le_trans y0 c y; above x' y in
3576
{qed}
3577
None
3578
end else begin
3579
{proof…}
3580
total y0 c; lt_not_minus_infinity c y0; pred_lt y0; pred_greatest y0 c;
3581
lt_le (pred_dz y0) y0;
3582
le_trans (pred_dz y0) y0 d;
3583
{qed}
3584
let yd = pred_diff_zero y0 in
3585
{proof…}
3586
total a (fp_add m x yd);
3587
if pure { le_dz a (fp_add m x yd) } then begin
3588
assert { le_dz y0 yd }; lt_not_le yd y0; absurd end;
3589
let lemma split (x' y: t) : unit
3590
requires { le_dz x x' /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3591
ensures { le_dz y yd }
3592
= le_not_nan c y; total y0 y;
3593
if pure { le_dz y0 y } then begin above x' y; absurd end;
3594
pred_greatest y0 y in
3595
{qed}
3596
if is_inf_f fm minf pinf yd then begin
3597
{proof…}
3598
let lemma pt (x' y: t) : unit
3599
requires { le_dz x x' /\ lt_dz x' (succ_dz x) }
3600
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3601
ensures { false }
3602
= split x' y; le_not_nan c y; le_not_nan c yd;
3603
assert { is_minus_infinity yd }; minus_infinity_unique yd;
3604
assert { index yd <= index y }; assert { y = yd };
3605
le_not_nan a (fp_add m x' y); add_special m x' y in
3606
let lemma pt2 (x' y: t) : unit
3607
requires { in_iv x' x h /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3608
ensures { false }
3609
= split x' y; le_not_nan c y; le_not_nan c yd;
3610
assert { is_minus_infinity yd }; minus_infinity_unique yd;
3611
assert { index yd <= index y }; assert { y = yd };
3612
le_not_nan a (fp_add m x' y); add_special m x' y in
3613
{qed}
3614
step_lo x h
3615
end else begin
3616
{proof…}
3617
let lemma below (x' y: t) : unit
3618
requires { le_dz x x' /\ lt_dz (fp_add m x' yd) a }
3619
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3620
ensures { false }
3621
= split x' y; le_not_nan a (fp_add m x' y); le_not_nan x x';
3622
add_special m x' yd; le_refl x'; add_box m x' x' y yd;
3623
le_lt_trans a (fp_add m x' y) (fp_add m x' yd); lt_not_le a a in
3624
{qed}
3625
let r = add_least fm minf pinf m x h a yd in
3626
{proof…}
3627
(match r with
3628
| None ->
3629
let lemma pt (x' y: t) : unit
3630
requires { in_iv x' x h /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3631
ensures { false }
3632
= le_not_nan x x'; add_special m x' yd; total a (fp_add m x' yd);
3633
below x' y in
3634
()
3635
| Some x1 ->
3636
let lemma pt (x' y: t) : unit
3637
requires { le_dz x x' /\ lt_dz x' x1 }
3638
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3639
ensures { false }
3640
= le_lt_trans x x' x1; lt_le x' x1; le_trans x' x1 h;
3641
le_not_nan x x'; add_special m x' yd; total a (fp_add m x' yd);
3642
if pure { le_dz a (fp_add m x' yd) } then lt_not_le x' x1;
3643
below x' y in
3644
()
3645
end);
3646
{qed}
3647
r
3648
end
3649
end
3650
end
3651
end
3652
end

A jump down from x in [l, x]: the floats of (result, x] have no solution; None when no float of [l, x] has one

  Valid (1.6s) (colibri2 119) (cvc4 3) (cvc5 12) (split_vc 2) (depth 2)
3656
let
jump_hi_iv
(ghost fm: format) (minf pinf: t) (m: mode) (l x a b c d: t)
3657
: option t
3658
requires { fmt minf = fm /\ fmt pinf = fm }
3659
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3660
requires { fmt x = fm /\ fmt l = fm /\ le_dz l x }
3661
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
3662
requires { le_dz a b /\ le_dz c d }
3663
ensures { match result with
3664
| None -> forall x'. in_iv x' l x -> no_cd m x' a b c d
3665
| Some x1 -> in_iv x1 l x /\
3666
forall x'. lt_dz x1 x' -> le_dz x' x -> no_cd m x' a b c d
3667
end }
3668
= {proof…} le_not_nan l x; le_not_nan a b; le_not_nan c d; le_refl x; {qed}
3669
if is_inf_f fm minf pinf x then begin
3670
if le_diff_zero a x && le_diff_zero x b then Some x
3671
else begin
3672
{proof…}
3673
let lemma inf (y: t) : unit
3674
requires { in_iv y c d /\ in_iv (fp_add m x y) a b }
3675
ensures { false }
3676
= le_not_nan c y; add_special m x y;
3677
if pure { is_plus_infinity x } then begin
3678
plus_infinity_unique x; plus_infinity_unique (fp_add m x y);
3679
assert { fp_add m x y = x }
3680
end else begin
3681
minus_infinity_unique x; minus_infinity_unique (fp_add m x y);
3682
assert { fp_add m x y = x }
3683
end in
3684
{qed}
3685
step_hi l x
3686
end
3687
end else begin
3688
{proof…} add_mono_x fm m x c d; {qed}
3689
match add_greatest fm minf pinf m c d b x with
3690
| None ->
3691
(* x + c > b *)
3692
if is_inf_f fm minf pinf c then begin
3693
{proof…}
3694
let lemma no (y: t) : unit
3695
requires { in_iv y c d /\ in_iv (fp_add m x y) a b } ensures { false }
3696
= le_not_nan a (fp_add m x y) in
3697
{qed}
3698
step_hi l x
3699
end else begin
3700
{proof…} le_refl c;
3701
let lemma above (x' y: t) : unit
3702
requires { le_dz x' x /\ lt_dz b (fp_add m x' c) }
3703
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3704
ensures { false }
3705
= le_not_nan a (fp_add m x' y); le_not_nan x' x; add_special m x' c;
3706
le_refl x'; add_box m x' x' c y;
3707
lt_le_trans b (fp_add m x' c) (fp_add m x' y); lt_not_le b b in
3708
{qed}
3709
let r = add_greatest fm minf pinf m l x b c in
3710
{proof…}
3711
(match r with
3712
| None ->
3713
let lemma pt (x' y: t) : unit
3714
requires { in_iv x' l x /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3715
ensures { false }
3716
= le_not_nan l x'; add_special m x' c; total (fp_add m x' c) b;
3717
above x' y in
3718
()
3719
| Some x1 ->
3720
let lemma pt (x' y: t) : unit
3721
requires { lt_dz x1 x' /\ le_dz x' x }
3722
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3723
ensures { false }
3724
= lt_le_trans x1 x' x; lt_le x1 x'; le_trans l x1 x';
3725
le_not_nan l x'; add_special m x' c; total (fp_add m x' c) b;
3726
if pure { le_dz (fp_add m x' c) b } then lt_not_le x1 x';
3727
above x' y in
3728
()
3729
end);
3730
{qed}
3731
r
3732
end
3733
| Some y1 ->
3734
let s = add m x y1 in
3735
if le_diff_zero a s then Some x
3736
else begin
3737
{proof…}
3738
le_not_nan y1 d; total a s;
3739
let lemma below (x' y: t) : unit
3740
requires { le_dz x' x /\ le_dz y y1 }
3741
requires { in_iv (fp_add m x' y) a b }
3742
ensures { false }
3743
= le_not_nan a (fp_add m x' y);
3744
add_box m x' x y y1; le_lt_trans (fp_add m x' y) s a;
3745
lt_not_le (fp_add m x' y) a in
3746
{qed}
3747
if le_diff_zero d y1 then begin
3748
{proof…}
3749
let lemma pt (x' y: t) : unit
3750
requires { in_iv x' l x /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3751
ensures { false }
3752
= le_trans y d y1; below x' y in
3753
{qed}
3754
None
3755
end else begin
3756
{proof…}
3757
total d y1; lt_not_plus_infinity y1 d; succ_gt y1; succ_least y1 d;
3758
le_lt_trans c y1 (succ_dz y1);
3759
lt_le y1 (succ_dz y1); le_trans c y1 (succ_dz y1);
3760
{qed}
3761
let yu = succ_diff_zero y1 in
3762
{proof…}
3763
total (fp_add m x yu) b;
3764
if pure { le_dz (fp_add m x yu) b } then begin
3765
assert { le_dz yu y1 }; lt_not_le y1 yu; absurd end;
3766
let lemma split (x' y: t) : unit
3767
requires { le_dz x' x /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3768
ensures { le_dz yu y }
3769
= le_not_nan c y; total y y1;
3770
if pure { le_dz y y1 } then begin below x' y; absurd end;
3771
succ_least y1 y in
3772
{qed}
3773
if is_inf_f fm minf pinf yu then begin
3774
{proof…}
3775
let lemma pt (x' y: t) : unit
3776
requires { lt_dz (pred_dz x) x' /\ le_dz x' x }
3777
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3778
ensures { false }
3779
= split x' y; le_not_nan c y; le_not_nan yu y;
3780
assert { is_plus_infinity yu }; plus_infinity_unique yu;
3781
assert { index y <= index yu }; assert { y = yu };
3782
le_not_nan a (fp_add m x' y); add_special m x' y in
3783
let lemma pt2 (x' y: t) : unit
3784
requires { in_iv x' l x /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3785
ensures { false }
3786
= split x' y; le_not_nan c y; le_not_nan yu y;
3787
assert { is_plus_infinity yu }; plus_infinity_unique yu;
3788
assert { index y <= index yu }; assert { y = yu };
3789
le_not_nan a (fp_add m x' y); add_special m x' y in
3790
{qed}
3791
step_hi l x
3792
end else begin
3793
{proof…}
3794
let lemma above (x' y: t) : unit
3795
requires { le_dz x' x /\ lt_dz b (fp_add m x' yu) }
3796
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3797
ensures { false }
3798
= split x' y; le_not_nan a (fp_add m x' y); le_not_nan x' x;
3799
add_special m x' yu; le_refl x'; add_box m x' x' yu y;
3800
lt_le_trans b (fp_add m x' yu) (fp_add m x' y); lt_not_le b b in
3801
{qed}
3802
let r = add_greatest fm minf pinf m l x b yu in
3803
{proof…}
3804
(match r with
3805
| None ->
3806
let lemma pt (x' y: t) : unit
3807
requires { in_iv x' l x /\ in_iv y c d /\ in_iv (fp_add m x' y) a b }
3808
ensures { false }
3809
= le_not_nan l x'; add_special m x' yu; total (fp_add m x' yu) b;
3810
above x' y in
3811
()
3812
| Some x1 ->
3813
let lemma pt (x' y: t) : unit
3814
requires { lt_dz x1 x' /\ le_dz x' x }
3815
requires { in_iv y c d /\ in_iv (fp_add m x' y) a b }
3816
ensures { false }
3817
= lt_le_trans x1 x' x; lt_le x1 x'; le_trans l x1 x';
3818
le_not_nan l x'; add_special m x' yu; total (fp_add m x' yu) b;
3819
if pure { le_dz (fp_add m x' yu) b } then lt_not_le x1 x';
3820
above x' y in
3821
()
3822
end);
3823
{qed}
3824
r
3825
end
3826
end
3827
end
3828
end
3829
end

The least of two jumps up

  Valid (26ms) (colibri2)
3832
let
min_jump
(x1 x2: option t) : option t
3833
requires { match x1, x2 with Some a, Some b -> is_not_nan a /\ is_not_nan b /\ fmt a = fmt b | _ -> true end }
3834
ensures { match x1, x2 with
3835
| None, _ -> result = x2 | _, None -> result = x1
3836
| Some a, Some b -> (result = x1 /\ le_dz a b) \/ (result = x2 /\ le_dz b a) end }
3837
= match x1, x2 with
3838
| None, _ -> x2
3839
| _, None -> x1
3840
| Some a, Some b ->
3841
if le_diff_zero a b then x1 else begin {proof…} total a b; lt_le b a; {qed} x2 end
3842
end

The greatest of two jumps down

  Valid (26ms) (colibri2)
3845
let
max_jump
(x1 x2: option t) : option t
3846
requires { match x1, x2 with Some a, Some b -> is_not_nan a /\ is_not_nan b /\ fmt a = fmt b | _ -> true end }
3847
ensures { match x1, x2 with
3848
| None, _ -> result = x2 | _, None -> result = x1
3849
| Some a, Some b -> (result = x1 /\ le_dz b a) \/ (result = x2 /\ le_dz a b) end }
3850
= match x1, x2 with
3851
| None, _ -> x2
3852
| _, None -> x1
3853
| Some a, Some b ->
3854
if le_diff_zero b a then x1 else begin {proof…} total b a; lt_le a b; {qed} x2 end
3855
end

The jump up for the y of l2

  Valid (1.6s) (colibri2 21) (cvc5) (split_vc) (depth 1)
3858
let rec
jump_lo_y
(ghost fm: format) (minf pinf: t) (m: mode) (x h a b: t)
3859
(l2: list (t, t)) : option t
3860
requires { fmt minf = fm /\ fmt pinf = fm }
3861
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3862
requires { fmt x = fm /\ fmt h = fm /\ le_dz x h }
3863
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l2 /\ fmt_u fm l2 }
3864
ensures { match result with
3865
| None -> forall x'. in_iv x' x h -> no_y m x' a b l2
3866
| Some x1 -> in_iv x1 x h /\
3867
forall x'. le_dz x x' -> lt_dz x' x1 -> no_y m x' a b l2
3868
end }
3869
variant { l2 }
3870
= match l2 with
3871
| Nil -> None
3872
| Cons (c, d) r ->
3873
let j1 = jump_lo_iv fm minf pinf m x h a b c d in
3874
let j2 = jump_lo_y fm minf pinf m x h a b r in
3875
{proof…}
3876
(match j1 with None -> () | Some x1 -> le_not_nan x x1 end);
3877
(match j2 with None -> () | Some x2 -> le_not_nan x x2 end);
3878
{qed}
3879
let res = min_jump j1 j2 in
3880
{proof…}
3881
(match j1, j2 with
3882
| Some x1, Some x2 ->
3883
let lemma pt (x': t) : unit
3884
ensures { le_dz x1 x2 -> lt_dz x' x1 -> lt_dz x' x2 }
3885
ensures { le_dz x2 x1 -> lt_dz x' x2 -> lt_dz x' x1 }
3886
= lt_le_trans_any x' x1 x2; lt_le_trans_any x' x2 x1 in ()
3887
| None, Some x2 ->
3888
let lemma pt (x': t) : unit
3889
requires { le_dz x x' /\ lt_dz x' x2 } ensures { in_iv x' x h }
3890
= lt_le x' x2; le_trans x' x2 h in ()
3891
| Some x1, None ->
3892
let lemma pt (x': t) : unit
3893
requires { le_dz x x' /\ lt_dz x' x1 } ensures { in_iv x' x h }
3894
= lt_le x' x1; le_trans x' x1 h in ()
3895
| None, None -> ()
3896
end);
3897
{qed}
3898
res
3899
end

The jump down for the y of l2

  Valid (746ms) (colibri2 21) (cvc5 3) (split_vc 2) (depth 2)
3902
let rec
jump_hi_y
(ghost fm: format) (minf pinf: t) (m: mode) (l x a b: t)
3903
(l2: list (t, t)) : option t
3904
requires { fmt minf = fm /\ fmt pinf = fm }
3905
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3906
requires { fmt x = fm /\ fmt l = fm /\ le_dz l x }
3907
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l2 /\ fmt_u fm l2 }
3908
ensures { match result with
3909
| None -> forall x'. in_iv x' l x -> no_y m x' a b l2
3910
| Some x1 -> in_iv x1 l x /\
3911
forall x'. lt_dz x1 x' -> le_dz x' x -> no_y m x' a b l2
3912
end }
3913
variant { l2 }
3914
= match l2 with
3915
| Nil -> None
3916
| Cons (c, d) r ->
3917
let j1 = jump_hi_iv fm minf pinf m l x a b c d in
3918
let j2 = jump_hi_y fm minf pinf m l x a b r in
3919
{proof…}
3920
(match j1 with None -> () | Some x1 -> le_not_nan x1 x end);
3921
(match j2 with None -> () | Some x2 -> le_not_nan x2 x end);
3922
{qed}
3923
let res = max_jump j1 j2 in
3924
{proof…}
3925
(match j1, j2 with
3926
| Some x1, Some x2 ->
3927
let lemma pt (x': t) : unit
3928
ensures { le_dz x2 x1 -> lt_dz x1 x' -> lt_dz x2 x' }
3929
ensures { le_dz x1 x2 -> lt_dz x2 x' -> lt_dz x1 x' }
3930
= le_lt_trans_any x2 x1 x'; le_lt_trans_any x1 x2 x' in ()
3931
| None, Some x2 ->
3932
let lemma pt (x': t) : unit
3933
requires { lt_dz x2 x' /\ le_dz x' x } ensures { in_iv x' l x }
3934
= lt_le x2 x'; le_trans l x2 x' in ()
3935
| Some x1, None ->
3936
let lemma pt (x': t) : unit
3937
requires { lt_dz x1 x' /\ le_dz x' x } ensures { in_iv x' l x }
3938
= lt_le x1 x'; le_trans l x1 x' in ()
3939
| None, None -> ()
3940
end);
3941
{qed}
3942
res
3943
end

The jump up for the sums of l1 and the y of l2

  Valid (1.5s) (cvc5)
3946
let rec
jump_lo_u
(ghost fm: format) (minf pinf: t) (m: mode) (x h: t)
3947
(l1 l2: list (t, t)) : option t
3948
requires { fmt minf = fm /\ fmt pinf = fm }
3949
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3950
requires { fmt x = fm /\ fmt h = fm /\ le_dz x h }
3951
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
3952
ensures { match result with
3953
| None -> forall x'. in_iv x' x h -> no_add m x' l1 l2
3954
| Some x1 -> in_iv x1 x h /\
3955
forall x'. le_dz x x' -> lt_dz x' x1 -> no_add m x' l1 l2
3956
end }
3957
variant { l1 }
3958
= match l1 with
3959
| Nil -> None
3960
| Cons (a, b) r ->
3961
let j1 = jump_lo_y fm minf pinf m x h a b l2 in
3962
let j2 = jump_lo_u fm minf pinf m x h r l2 in
3963
{proof…}
3964
(match j1 with None -> () | Some x1 -> le_not_nan x x1 end);
3965
(match j2 with None -> () | Some x2 -> le_not_nan x x2 end);
3966
{qed}
3967
let res = min_jump j1 j2 in
3968
{proof…}
3969
(match j1, j2 with
3970
| Some x1, Some x2 ->
3971
let lemma pt (x': t) : unit
3972
ensures { le_dz x1 x2 -> lt_dz x' x1 -> lt_dz x' x2 }
3973
ensures { le_dz x2 x1 -> lt_dz x' x2 -> lt_dz x' x1 }
3974
= lt_le_trans_any x' x1 x2; lt_le_trans_any x' x2 x1 in ()
3975
| None, Some x2 ->
3976
let lemma pt (x': t) : unit
3977
requires { le_dz x x' /\ lt_dz x' x2 } ensures { in_iv x' x h }
3978
= lt_le x' x2; le_trans x' x2 h in ()
3979
| Some x1, None ->
3980
let lemma pt (x': t) : unit
3981
requires { le_dz x x' /\ lt_dz x' x1 } ensures { in_iv x' x h }
3982
= lt_le x' x1; le_trans x' x1 h in ()
3983
| None, None -> ()
3984
end);
3985
{qed}
3986
res
3987
end

The jump down for the sums of l1 and the y of l2

  Valid (332ms) (colibri2 21) (cvc5 2) (split_vc 2) (depth 2)
3990
let rec
jump_hi_u
(ghost fm: format) (minf pinf: t) (m: mode) (l x: t)
3991
(l1 l2: list (t, t)) : option t
3992
requires { fmt minf = fm /\ fmt pinf = fm }
3993
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
3994
requires { fmt x = fm /\ fmt l = fm /\ le_dz l x }
3995
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
3996
ensures { match result with
3997
| None -> forall x'. in_iv x' l x -> no_add m x' l1 l2
3998
| Some x1 -> in_iv x1 l x /\
3999
forall x'. lt_dz x1 x' -> le_dz x' x -> no_add m x' l1 l2
4000
end }
4001
variant { l1 }
4002
= match l1 with
4003
| Nil -> None
4004
| Cons (a, b) r ->
4005
let j1 = jump_hi_y fm minf pinf m l x a b l2 in
4006
let j2 = jump_hi_u fm minf pinf m l x r l2 in
4007
{proof…}
4008
(match j1 with None -> () | Some x1 -> le_not_nan x1 x end);
4009
(match j2 with None -> () | Some x2 -> le_not_nan x2 x end);
4010
{qed}
4011
let res = max_jump j1 j2 in
4012
{proof…}
4013
(match j1, j2 with
4014
| Some x1, Some x2 ->
4015
let lemma pt (x': t) : unit
4016
ensures { le_dz x2 x1 -> lt_dz x1 x' -> lt_dz x2 x' }
4017
ensures { le_dz x1 x2 -> lt_dz x2 x' -> lt_dz x1 x' }
4018
= le_lt_trans_any x2 x1 x'; le_lt_trans_any x1 x2 x' in ()
4019
| None, Some x2 ->
4020
let lemma pt (x': t) : unit
4021
requires { lt_dz x2 x' /\ le_dz x' x } ensures { in_iv x' l x }
4022
= lt_le x2 x'; le_trans l x2 x' in ()
4023
| Some x1, None ->
4024
let lemma pt (x': t) : unit
4025
requires { lt_dz x1 x' /\ le_dz x' x } ensures { in_iv x' l x }
4026
= lt_le x1 x'; le_trans l x1 x' in ()
4027
| None, None -> ()
4028
end);
4029
{qed}
4030
res
4031
end

The least x' of [l, h] such that the floats of [l, x') are no_add, after at most fuel jumps; None when all [l, h] is

  Valid (633ms) (cvc5)
4035
let rec
shave_lo
(ghost fm: format) (minf pinf: t) (m: mode)
4036
(l1 l2: list (t, t)) (fuel: int63) (l h: t) : option t
4037
requires { fmt minf = fm /\ fmt pinf = fm }
4038
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4039
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
4040
requires { fmt l = fm /\ fmt h = fm /\ le_dz l h }
4041
ensures { match result with
4042
| None -> forall x. in_iv x l h -> no_add m x l1 l2
4043
| Some l' -> in_iv l' l h /\
4044
forall x. le_dz l x -> lt_dz x l' -> no_add m x l1 l2 end }
4045
variant { fuel }
4046
= {proof…} le_not_nan l h; le_refl l; {qed}
4047
if fuel <= 0 then begin
4048
{proof…}
4049
let lemma no (x: t) : unit
4050
requires { le_dz l x /\ lt_dz x l } ensures { false }
4051
= lt_not_le x l in
4052
{qed}
4053
Some l
4054
end else
4055
match jump_lo_u fm minf pinf m l h l1 l2 with
4056
| None -> None
4057
| Some l' ->
4058
if le_diff_zero l' l then begin
4059
{proof…}
4060
let lemma no (x: t) : unit
4061
requires { le_dz l x /\ lt_dz x l } ensures { false }
4062
= lt_not_le x l in
4063
{qed}
4064
Some l
4065
end else begin
4066
let res = shave_lo fm minf pinf m l1 l2 (fuel - 1) l' h in
4067
{proof…}
4068
let lemma split (x: t) : unit
4069
requires { le_dz l x } ensures { lt_dz x l' \/ le_dz l' x }
4070
= le_not_nan l x; le_not_nan l l'; total l' x;
4071
lt_le_any x l'; total_any x l' in
4072
(match res with
4073
| None ->
4074
let lemma pt (x: t) : unit
4075
requires { in_iv x l h } ensures { no_add m x l1 l2 }
4076
= split x in ()
4077
| Some l'' ->
4078
le_trans l l' l'';
4079
let lemma pt (x: t) : unit
4080
requires { le_dz l x /\ lt_dz x l'' } ensures { no_add m x l1 l2 }
4081
= split x in ()
4082
end);
4083
{qed}
4084
res
4085
end
4086
end

The greatest x' of [l, h] such that the floats of (x', h] are no_add, symmetric to shave_lo

  Valid (638ms) (cvc5)
4090
let rec
shave_hi
(ghost fm: format) (minf pinf: t) (m: mode)
4091
(l1 l2: list (t, t)) (fuel: int63) (l h: t) : option t
4092
requires { fmt minf = fm /\ fmt pinf = fm }
4093
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4094
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
4095
requires { fmt l = fm /\ fmt h = fm /\ le_dz l h }
4096
ensures { match result with
4097
| None -> forall x. in_iv x l h -> no_add m x l1 l2
4098
| Some h' -> in_iv h' l h /\
4099
forall x. lt_dz h' x -> le_dz x h -> no_add m x l1 l2 end }
4100
variant { fuel }
4101
= {proof…} le_not_nan l h; le_refl h; {qed}
4102
if fuel <= 0 then begin
4103
{proof…}
4104
let lemma no (x: t) : unit
4105
requires { lt_dz h x /\ le_dz x h } ensures { false }
4106
= lt_not_le h x in
4107
{qed}
4108
Some h
4109
end else
4110
match jump_hi_u fm minf pinf m l h l1 l2 with
4111
| None -> None
4112
| Some h' ->
4113
if le_diff_zero h h' then begin
4114
{proof…}
4115
let lemma no (x: t) : unit
4116
requires { lt_dz h x /\ le_dz x h } ensures { false }
4117
= lt_not_le h x in
4118
{qed}
4119
Some h
4120
end else begin
4121
let res = shave_hi fm minf pinf m l1 l2 (fuel - 1) l h' in
4122
{proof…}
4123
let lemma split (x: t) : unit
4124
requires { le_dz x h } ensures { lt_dz h' x \/ le_dz x h' }
4125
= le_not_nan x h; le_not_nan h' h; total x h';
4126
lt_le_any h' x; total_any h' x in
4127
(match res with
4128
| None ->
4129
let lemma pt (x: t) : unit
4130
requires { in_iv x l h } ensures { no_add m x l1 l2 }
4131
= split x in ()
4132
| Some h'' ->
4133
le_trans h'' h' h;
4134
let lemma pt (x: t) : unit
4135
requires { lt_dz h'' x /\ le_dz x h } ensures { no_add m x l1 l2 }
4136
= split x in ()
4137
end);
4138
{qed}
4139
res
4140
end
4141
end

The steps of the shaving at each end

4144
let constant
shave_fuel
: int63 = 4

The floats of a list, ends shaved

  Valid (381ms) (colibri2 29) (cvc4) (cvc5 4) (split_vc) (depth 1)
4147
let rec
shave_u
(ghost fm: format) (minf pinf: t) (m: mode)
4148
(l1 l2 xs: list (t, t)) : list (t, t)
4149
requires { fmt minf = fm /\ fmt pinf = fm }
4150
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4151
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
4152
requires { ok_u xs /\ fmt_u fm xs }
4153
ensures { ok_u result /\ fmt_u fm result }
4154
ensures { forall v. mem_u v result -> mem_u v xs }
4155
ensures { forall v. mem_u v xs -> not no_add m v l1 l2 -> mem_u v result }
4156
variant { xs }
4157
= match xs with
4158
| Nil -> Nil
4159
| Cons (a, b) r ->
4160
let r' = shave_u fm minf pinf m l1 l2 r in
4161
{proof…}
4162
(match r with Nil -> () | Cons _ _ -> sorted_above b r end);
4163
assert { above b r' };
4164
above_head b r';
4165
{qed}
4166
match shave_lo fm minf pinf m l1 l2 shave_fuel a b with
4167
| None -> r'
4168
| Some a' ->
4169
match shave_hi fm minf pinf m l1 l2 shave_fuel a' b with
4170
| None ->
4171
{proof…}
4172
let lemma pt (v: t) : unit
4173
requires { in_iv v a b } ensures { no_add m v l1 l2 }
4174
= le_not_nan a v; total v a';
4175
if pure { le_dz a' v } then () in
4176
{qed}
4177
r'
4178
| Some b' ->
4179
{proof…}
4180
le_trans a a' b';
4181
(match r' with Nil -> () | Cons (c, _) _ -> le_lt_trans b' b c end);
4182
let lemma pt (v: t) : unit
4183
requires { in_iv v a b /\ not no_add m v l1 l2 } ensures { in_iv v a' b' }
4184
= le_not_nan a v; total v a'; total b' v;
4185
if pure { lt_dz v a' } then absurd;
4186
if pure { lt_dz b' v } then absurd in
4187
let lemma inc (v: t) : unit
4188
requires { in_iv v a' b' } ensures { in_iv v a b }
4189
= le_trans a a' v; le_trans v b' b in
4190
{qed}
4191
Cons (a', b') r'
4192
end
4193
end
4194
end

The x of d, ends shaved: every x of d with x + y in r for some y of e is kept. Nothing is shaved when r has NaN.

  Valid (424ms) (cvc5)
4198
let
add_shave
(ghost fm: format) (minf pinf: t) (m: mode) (r e d: dom) : dom
4199
requires { fmt minf = fm /\ fmt pinf = fm }
4200
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4201
requires { r.fm = fm /\ e.fm = fm /\ d.fm = fm }
4202
ensures { result.fm = fm }
4203
ensures { forall v. mem v result -> mem v d }
4204
ensures { forall x y. mem x d -> mem y e -> mem (fp_add m x y) r -> mem x result }
4205
= if r.nan then d
4206
else begin
4207
let u = shave_u fm minf pinf m r.u e.u d.u in
4208
let res = { u = u; nan = d.nan; fm = fm } in
4209
{proof…}
4210
let lemma pt (x y: t) : unit
4211
requires { mem x d /\ mem y e /\ mem (fp_add m x y) r }
4212
ensures { mem x res }
4213
= add_special m x y;
4214
if pure { is_not_nan x } then begin
4215
assert { mem_u y e.u };
4216
assert { not no_add m x r.u e.u }
4217
end in
4218
{qed}
4219
res
4220
end
Backward absolute value

Exact: the x such that abs x is in d, that is the positive floats of d and their opposites (and NaN).

  Valid (659ms) (colibri2 12) (cvc5 3) (split_vc) (depth 1)
4226
let
abs_back
(ghost fm: format) (pz pinf: t) (d: dom) : dom
4227
requires { fmt pz = fm /\ fmt pinf = fm /\ d.fm = fm }
4228
ensures { result.fm = fm }
4229
requires { at_index fm pz 0 /\ pinf = (plus_infinity fm) }
4230
ensures { forall x. mem x result <-> mem (abs x) d }
4231
= let pos = match inter fm d (positives fm pz pinf) with
4232
| None -> (empty_dom fm)
4233
| Some p -> union fm p (neg_dom fm p)
4234
end in
4235
let r = if d.nan then union fm (nan_dom fm) pos else pos in
4236
{proof…}
4237
let lemma pt (x: t) : unit
4238
ensures { mem x r <-> mem (abs x) d }
4239
= if is_nan x then assert { is_nan (abs x) }
4240
else begin
4241
index_range x;
4242
if pure { is_positive x } then begin
4243
assert { abs x = x by of_index fm (index (abs x)) = of_index fm (index x) };
4244
assert { not is_positive (neg x) }
4245
end else begin
4246
neg_neg x;
4247
assert { abs x = neg x by
4248
of_index fm (index (abs x)) = of_index fm (index (neg x)) };
4249
assert { is_positive (neg x) }
4250
end
4251
end in
4252
{qed}
4253
r
Backward square root

The positive x whose square root is in [a, b]: above pa * pa rounded down, below sb * sb rounded up, where pa is the float below a and sb the float above b (Facts.sqrt_lower, sqrt_upper). The negative x are –0, whose square root is computed, and the ones whose square root is NaN.

The least positive float y >= c whose square root is at least a, from a lower bound c of those floats

  Valid (988ms) (colibri2)
4265
let rec
sqrt_lo
(ghost fm: format) (pinf: t) (m: mode) (a c: t) : t
4266
requires { fmt pinf = fm /\ pinf = plus_infinity fm /\ fmt a = fm /\ fmt c = fm }
4267
requires { is_not_nan a /\ is_not_nan c /\ is_positive c }
4268
requires { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4269
le_dz a (fp_sqrt m x) -> le_dz c x }
4270
ensures { is_not_nan result /\ is_positive result /\ fmt result = fm }
4271
ensures { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4272
le_dz a (fp_sqrt m x) -> le_dz result x }
4273
variant { index pinf - index c }
4274
= {proof…}
4275
sqrt_positive m c; index_range c; index_range pinf;
4276
{qed}
4277
if lt_diff_zero (sqrt m c) a then begin
4278
{proof…}
4279
if pure { is_plus_infinity c } then begin
4280
assert { ext c = PInf };
4281
assert { fp_sqrt m c = plus_infinity fm };
4282
lt_not_plus_infinity (fp_sqrt m c) a; absurd
4283
end;
4284
succ_gt c; index_range (succ_dz c);
4285
let lemma pt (x: t) : unit
4286
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ le_dz a (fp_sqrt m x) }
4287
ensures { le_dz (succ_dz c) x }
4288
= total x c;
4289
if pure { le_dz x c } then begin
4290
sqrt_positive m x;
4291
sqrt_le m x c;
4292
le_lt_trans (fp_sqrt m x) (fp_sqrt m c) a;
4293
lt_not_le (fp_sqrt m x) a;
4294
absurd
4295
end;
4296
succ_least c x in
4297
le_positive c (succ_dz c);
4298
{qed}
4299
sqrt_lo fm pinf m a (succ_diff_zero c)
4300
end else c

The greatest float y <= c whose square root is at most b (or a negative float), from an upper bound c of the positive ones

  Valid (520ms) (cvc4)
4304
let rec
sqrt_hi
(ghost fm: format) (pz: t) (m: mode) (b c: t) : t
4305
requires { fmt pz = fm /\ at_index fm pz 0 /\ fmt b = fm /\ fmt c = fm }
4306
requires { is_not_nan b /\ is_not_nan c }
4307
requires { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4308
le_dz (fp_sqrt m x) b -> le_dz x c }
4309
ensures { is_not_nan result /\ fmt result = fm }
4310
ensures { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4311
le_dz (fp_sqrt m x) b -> le_dz x result }
4312
variant { index c }
4313
= {proof…}
4314
index_range c;
4315
{qed}
4316
if lt_diff_zero c pz then c
4317
else begin
4318
{proof…} sqrt_positive m c; {qed}
4319
if lt_diff_zero b (sqrt m c) then begin
4320
{proof…}
4321
assert { 0 <= index c };
4322
if pure { is_minus_infinity c } then absurd;
4323
pred_lt c; index_range (pred_dz c);
4324
let lemma pt (x: t) : unit
4325
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ le_dz (fp_sqrt m x) b }
4326
ensures { le_dz x (pred_dz c) }
4327
= total c x;
4328
if pure { le_dz c x } then begin
4329
sqrt_positive m x;
4330
sqrt_le m c x;
4331
lt_le_trans b (fp_sqrt m c) (fp_sqrt m x);
4332
lt_not_le b (fp_sqrt m x);
4333
absurd
4334
end;
4335
pred_greatest c x in
4336
{qed}
4337
sqrt_hi fm pz m b (pred_diff_zero c)
4338
end else c
4339
end
4340
Valid (1.7s) (colibri2 51) (cvc4 4) (cvc5 14) (split_vc) (depth 1)
4341
let
sqrt_back_iv
(ghost fm: format) (pinf pz: t) (m: mode) (a b: t) : dom
4342
requires { fmt pinf = fm /\ fmt pz = fm }
4343
requires { pinf = plus_infinity fm /\ at_index fm pz 0 }
4344
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b }
4345
ensures { result.fm = fm }
4346
ensures { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4347
in_iv (fp_sqrt m x) a b -> mem x result }
4348
= {proof…}
4349
le_not_nan a b; index_range a; index_range b; index_range pinf;
4350
{qed}
4351
let lo =
4352
if le_diff_zero a pz then pz
4353
else begin
4354
let pa = pred_diff_zero a in
4355
{proof…}
4356
pred_lt a; of_index_range fm (index a - 1); index_range pa;
4357
assert { is_positive pa };
4358
mul_special rtn pa pa;
4359
{qed}
4360
let l = mul rtn pa pa in
4361
{proof…}
4362
round_f_down fm false (emul (ext pa) (ext pa));
4363
if pure { is_plus_infinity l } then begin
4364
assert { ext l = PInf };
4365
assert { is_plus_infinity pa };
4366
lt_not_plus_infinity pa a; absurd
4367
end;
4368
succ_gt l;
4369
let lemma pt (x: t) : unit
4370
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ le_dz a (fp_sqrt m x) }
4371
ensures { le_dz (succ_dz l) x }
4372
= sqrt_lower m pa a x; succ_least l x in
4373
round_f_le fm rtn false false (Fin 0.0) (emul (ext pa) (ext pa));
4374
le_positive (round_f fm rtn false (Fin 0.0)) l;
4375
le_positive l (succ_dz l);
4376
{qed}
4377
sqrt_lo fm pinf m a (succ_diff_zero l)
4378
end in
4379
let hi =
4380
if is_pinf fm pinf b then Some pinf
4381
else if lt_diff_zero b pz then None
4382
else begin
4383
let sb = succ_diff_zero b in
4384
{proof…}
4385
succ_gt b; of_index_range fm (index b + 1); index_range sb;
4386
assert { is_positive sb };
4387
mul_special rtp sb sb;
4388
{qed}
4389
let u = mul rtp sb sb in
4390
{proof…}
4391
round_f_up fm false (emul (ext sb) (ext sb));
4392
ext_sign sb;
4393
if pure { is_minus_infinity u } then begin
4394
assert { ext u = MInf };
4395
absurd
4396
end;
4397
pred_lt u;
4398
let lemma pt (x: t) : unit
4399
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ le_dz (fp_sqrt m x) b }
4400
ensures { le_dz x (pred_dz u) }
4401
= sqrt_upper m sb b x; pred_greatest u x in
4402
{qed}
4403
Some (sqrt_hi fm pz m b (pred_diff_zero u))
4404
end in
4405
match hi with
4406
| Some hi ->
4407
{proof…}
4408
let lemma pt (x: t) : unit
4409
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ in_iv (fp_sqrt m x) a b }
4410
ensures { le_dz lo x /\ le_dz x hi }
4411
= index_range x;
4412
if pure { is_plus_infinity b } then begin
4413
if pure { is_plus_infinity x } then begin
4414
plus_infinity_unique x; le_refl x
4415
end else begin
4416
lt_plus_infinity x; lt_le x pinf
4417
end
4418
end in
4419
{qed}
4420
if lt_diff_zero hi lo then begin
4421
{proof…}
4422
let lemma no (x: t) : unit
4423
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ in_iv (fp_sqrt m x) a b }
4424
ensures { false }
4425
= pt x; le_trans lo x hi; lt_not_le hi lo in
4426
{qed}
4427
empty_dom fm
4428
end else begin
4429
{proof…} total lo hi; {qed}
4430
interval fm lo hi
4431
end
4432
| None ->
4433
{proof…}
4434
let lemma no (x: t) : unit
4435
requires { fmt x = fm /\ is_not_nan x /\ is_positive x /\ in_iv (fp_sqrt m x) a b }
4436
ensures { false }
4437
= sqrt_positive m x; le_positive (fp_sqrt m x) b in
4438
{qed}
4439
empty_dom fm
4440
end
4441
Valid (293ms) (cvc5)
4442
let rec
sqrt_back_u
(ghost fm: format) (pinf pz: t) (m: mode) (l: list (t, t)) : dom
4443
requires { fmt pinf = fm /\ fmt pz = fm }
4444
requires { pinf = plus_infinity fm /\ at_index fm pz 0 }
4445
requires { ok_u l /\ fmt_u fm l }
4446
ensures { result.fm = fm }
4447
ensures { forall x. fmt x = fm -> is_not_nan x -> is_positive x ->
4448
mem_u (fp_sqrt m x) l -> mem x result }
4449
variant { l }
4450
= match l with
4451
| Nil -> empty_dom fm
4452
| Cons (a, b) r ->
4453
union fm (sqrt_back_iv fm pinf pz m a b) (sqrt_back_u fm pinf pz m r)
4454
end

The x such that sqrt x is in r

  Valid (759ms) (colibri2 26) (cvc4) (cvc5 4) (split_vc) (depth 1)
4457
let
sqrt_back
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r: dom) : dom
4458
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
4459
requires { specials4 fm minf pinf nz pz }
4460
requires { r.fm = fm }
4461
ensures { result.fm = fm }
4462
ensures { forall x. fmt x = fm -> mem (fp_sqrt m x) r -> mem x result }
4463
= {proof…}
4464
format_facts fm (); index_range nz; index_range minf; of_index_range fm (-2);
4465
{qed}
4466
let u = sqrt_back_u fm pinf pz m r.u in
4467
let s = sqrt m nz in
4468
{proof…} sqrt_special m nz; index_range s; {qed}
4469
let z = if meets fm s s r then singleton fm nz else empty_dom fm in
4470
let n =
4471
if r.nan then begin
4472
let pnz = pred_diff_zero nz in
4473
{proof…} pred_lt nz; minus_infinity_lt pnz; lt_le minf pnz; {qed}
4474
union fm (nan_dom fm) (interval fm minf pnz)
4475
end else empty_dom fm in
4476
let res = union fm n (union fm z u) in
4477
{proof…}
4478
let lemma pt (x: t) : unit
4479
requires { fmt x = fm /\ mem (fp_sqrt m x) r }
4480
ensures { mem x res }
4481
= sqrt_special m x;
4482
if pure { is_not_nan x } then begin
4483
index_range x;
4484
if pure { is_positive x } then ()
4485
else if pure { is_zero x } then begin
4486
assert { index x = -1 };
4487
assert { x = nz by of_index fm (index x) = of_index fm (index nz) };
4488
assert { index (fp_sqrt m x) = index s }
4489
end else begin
4490
assert { index x <= -2 };
4491
minus_infinity_unique minf; index_range minf;
4492
assert { in_iv x minf (pred_dz nz) }
4493
end
4494
end in
4495
{qed}
4496
res
Backward rounding to an integral float

Direct, as add_lo: the floats x with a <= roundToIntegral x have round_int m x at or above t = thr_lo a (in the format with one more bit), so at or above n = round_int RTP t, so x is at or above int_thr_lo m n, rounded up in the format. One step up when that float itself rounds below a (the ties of RNE and RNA, the strict bounds of RTP and RTZ). Sound for any mode; exact in the usual cases (tested against the floats of small formats).

The value of x, finite

  Valid (36ms) (colibri2)
4509
let ghost
fin_val
(x: t) : real
4510
requires { is_not_nan x /\ not is_infinity x }
4511
ensures { ext x = Fin result /\ to_real x = result }
4512
= {proof…} match ext x with Fin v -> v | _ -> absurd end {qed}

At most fuel steps up from c, a lower bound of the x with a <= roundToIntegral x, while c itself rounds below a

  Valid (127ms) (cvc5)
4516
let rec
rti_up
(ghost fm: format) (pinf: t) (m: mode) (a c: t) (fuel: int) : t
4517
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
4518
requires { fmt a = fm /\ is_not_nan a /\ fmt c = fm /\ is_not_nan c }
4519
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_rti m x) -> le_dz c x }
4520
ensures { fmt result = fm /\ is_not_nan result }
4521
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_rti m x) -> le_dz result x }
4522
variant { fuel }
4523
= if fuel <= 0 || is_pinf fm pinf c then c
4524
else if lt_diff_zero (round_to_integral m c) a then begin
4525
{proof…}
4526
let lemma step (x: t) : unit
4527
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_rti m x) }
4528
ensures { le_dz (succ_dz c) x }
4529
= if pure { x = c } then begin lt_not_le (fp_rti m c) a; absurd end;
4530
index_range x; index_range c;
4531
assert { index x <> index c };
4532
assert { lt_dz c x };
4533
succ_least c x in
4534
succ_gt c;
4535
{qed}
4536
rti_up fm pinf m a (succ_diff_zero c) (fuel - 1)
4537
end else c

At most fuel steps down from c, an upper bound of the x with roundToIntegral x <= b, while c itself rounds above b

  Valid (114ms) (cvc5)
4541
let rec
rti_down
(ghost fm: format) (minf: t) (m: mode) (b c: t) (fuel: int) : t
4542
requires { fmt minf = fm /\ minf = minus_infinity fm }
4543
requires { fmt b = fm /\ is_not_nan b /\ fmt c = fm /\ is_not_nan c }
4544
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_rti m x) b -> le_dz x c }
4545
ensures { fmt result = fm /\ is_not_nan result }
4546
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_rti m x) b -> le_dz x result }
4547
variant { fuel }
4548
= if fuel <= 0 || is_minf fm minf c then c
4549
else if lt_diff_zero b (round_to_integral m c) then begin
4550
{proof…}
4551
let lemma step (x: t) : unit
4552
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_rti m x) b }
4553
ensures { le_dz x (pred_dz c) }
4554
= if pure { x = c } then begin lt_not_le b (fp_rti m c); absurd end;
4555
index_range x; index_range c;
4556
assert { index x <> index c };
4557
assert { lt_dz x c };
4558
pred_greatest c x in
4559
pred_lt c;
4560
{qed}
4561
rti_down fm minf m b (pred_diff_zero c) (fuel - 1)
4562
end else c
4563
Valid (463ms) (colibri2 77) (cvc4) (cvc5 7) (split_vc) (depth 1)
4564
let
rti_lo
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (a: t) : t
4565
requires { widens fm fm' /\ fmt a = fm /\ fmt minf = fm /\ fmt pinf = fm }
4566
requires { fmt nz = fm /\ fmt pz = fm }
4567
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4568
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ is_not_nan a }
4569
ensures { fmt result = fm /\ is_not_nan result }
4570
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_rti m x) -> le_dz result x }
4571
= if is_minf fm minf a then begin
4572
{proof…}
4573
let lemma pt (x: t) : unit
4574
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz minf x }
4575
= index_range x; index_range minf in
4576
{qed}
4577
minf
4578
end else if is_pz fm pz a && lt_diff_zero (round_to_integral m nz) a then begin
4580
(* `roundToIntegral` keeps the sign of a zero: only the positive floats reach +0 *)
4581
{proof…}
4582
let lemma pt (x: t) : unit
4583
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_rti m x) } ensures { le_dz pz x }
4584
= index_range x;
4585
if pure { index x < 0 } then begin
4586
assert { le_dz x nz };
4587
rti_le m x nz; le_trans a (fp_rti m x) (fp_rti m nz);
4588
lt_not_le (fp_rti m nz) a; absurd
4589
end in
4590
{qed}
4591
pz
4592
end else begin
4593
let fr = format_of a in
4594
let t = thr_lo fm fm' nz pz m a in
4595
let minf' = convert fm' rtn minf in
4596
let pinf' = convert fm' rtn pinf in
4597
{proof…}
4598
assert { ext minf' = MInf /\ ext pinf' = PInf };
4599
convert_not_nan fm' rtn minf; convert_not_nan fm' rtn pinf;
4600
(* the floats `x` that reach `a`: their integer is above `t` *)
4601
let lemma reach (x: t) : unit
4602
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_rti m x) }
4603
ensures { not is_minus_infinity x }
4604
ensures { not is_infinity x -> ele (ext t) (Fin (from_int (round_int m (to_real x)))) }
4605
= if pure { is_minus_infinity x } then begin
4606
assert { ext (fp_rti m x) = MInf };
4607
ext_le a (fp_rti m x); absurd
4608
end in
4609
{qed}
4610
if le_diff_zero t minf' then begin
4611
{proof…}
4612
let lemma pt (x: t) : unit
4613
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz minf x }
4614
= index_range x; index_range minf in
4615
{qed}
4616
rti_up fm pinf m a minf 2
4617
end else if le_diff_zero pinf' t then begin
4618
{proof…}
4619
let lemma pt (x: t) : unit
4620
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_rti m x) }
4621
ensures { le_dz pinf x }
4622
= reach x;
4623
if pure { not is_infinity x } then begin
4624
index_range t; index_range pinf'; assert { ext t = PInf }; absurd
4625
end in
4626
{qed}
4627
pinf
4628
end else begin
4629
{proof…}
4630
index_range t; index_range pinf'; index_range minf'; assert { not is_infinity t };
4631
{qed}
4632
let a0 = if is_zero_f fm nz pz a then nz else a in
4633
let ct = convert fr m t in
4634
let strict = not (le_diff_zero a0 ct) in
4635
let n = if strict then round_int_v rtn t + 1 else round_int_v rtp t in
4636
{proof…}
4637
convert_not_nan fm m t;
4638
assert { le_dz a0 a /\ fmt a0 = fm /\ is_not_nan a0 };
4639
(* `t` rounds below `a0`: the integers are strictly above `t` *)
4640
let lemma nk (x: t) : unit
4641
requires { fmt x = fm /\ is_not_nan x /\ not is_infinity x /\ le_dz a (fp_rti m x) }
4642
ensures { n <= round_int m (to_real x) }
4643
= reach x;
4644
let ghost r = fin_val x in
4645
let ghost tt = fin_val t in
4646
let ghost k = round_int m r in
4647
if strict then begin
4648
if pure { from_int k = tt } then begin
4649
let ghost zx = if is_positive x then false else true in
4650
let ghost zt = if is_positive t then false else true in
4651
assert { fp_rti m x = round_f fm m zx (ext t) };
4652
assert { ct = round_f fm m zt (ext t) };
4653
round_f_val fm m zx zt (ext t);
4654
total a0 ct;
4655
assert { lt_dz ct a0 };
4656
assert { not (is_zero ct /\ is_zero a0) by is_zero a0 -> a0 = nz };
4657
assert { not ele (ext a0) (ext ct) };
4658
ext_le a (fp_rti m x); ext_le a0 a;
4659
absurd
4660
end;
4661
round_int_ge RTN tt (round_int RTN tt)
4662
end else round_int_ceil tt k in
4663
{qed}
4664
let k = match m with
4665
| RTN -> 2 * n
4666
| RTP -> 2 * n - 2
4667
| RTZ -> if 0 < n then 2 * n else 2 * n - 2
4668
| RNE | RNA -> 2 * n - 1
4669
end in
4670
let c0 = of_q fr rtp true k 2 in
4671
let c = if is_zero_f fm nz pz c0 then nz else c0 in
4672
{proof…}
4673
assert { from_int k /. 2.0 = int_thr_lo m n };
4674
let lemma above (x: t) : unit
4675
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_rti m x) }
4676
ensures { le_dz c x }
4677
= reach x;
4678
if pure { is_plus_infinity x } then begin
4679
index_range x; index_range c
4680
end else begin
4681
let ghost r = fin_val x in
4682
let ghost tt = fin_val t in
4683
nk x;
4684
round_int_ge m r n;
4685
let ghost zx = if is_positive x then false else true in
4686
convert_exact rtp x;
4687
assert { round_f fm rtp zx (ext x) = x };
4688
round_f_ele fm rtp true zx (Fin (from_int k /. 2.0)) (ext x);
4689
assert { ele (ext c) (ext x) };
4690
ele_le c x
4691
end in
4692
{qed}
4693
rti_up fm pinf m a c 2
4694
end
4695
end
4696
Valid (413ms) (colibri2 77) (cvc4) (cvc5 7) (split_vc) (depth 1)
4697
let
rti_hi
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (b: t) : t
4698
requires { widens fm fm' /\ fmt b = fm /\ fmt minf = fm /\ fmt pinf = fm }
4699
requires { fmt nz = fm /\ fmt pz = fm }
4700
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4701
requires { at_index fm nz (-1) /\ at_index fm pz 0 /\ is_not_nan b }
4702
ensures { fmt result = fm /\ is_not_nan result }
4703
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_rti m x) b -> le_dz x result }
4704
= if is_pinf fm pinf b then begin
4705
{proof…}
4706
let lemma pt (x: t) : unit
4707
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz x pinf }
4708
= index_range x; index_range pinf in
4709
{qed}
4710
pinf
4711
end else if is_nz fm nz b && lt_diff_zero b (round_to_integral m pz) then begin
4712
(* only the negative floats reach -0 or below *)
4713
{proof…}
4714
let lemma pt (x: t) : unit
4715
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_rti m x) b } ensures { le_dz x nz }
4716
= index_range x;
4717
if pure { 0 <= index x } then begin
4718
assert { le_dz pz x };
4719
rti_le m pz x; le_trans (fp_rti m pz) (fp_rti m x) b;
4720
lt_not_le b (fp_rti m pz); absurd
4721
end in
4722
{qed}
4723
nz
4724
end else begin
4725
let fr = format_of b in
4726
let t = thr_hi fm fm' nz pz m b in
4727
let minf' = convert fm' rtn minf in
4728
let pinf' = convert fm' rtn pinf in
4729
{proof…}
4730
assert { ext minf' = MInf /\ ext pinf' = PInf };
4731
convert_not_nan fm' rtn minf; convert_not_nan fm' rtn pinf;
4732
let lemma reach (x: t) : unit
4733
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_rti m x) b }
4734
ensures { not is_plus_infinity x }
4735
ensures { not is_infinity x -> ele (Fin (from_int (round_int m (to_real x)))) (ext t) }
4736
= if pure { is_plus_infinity x } then begin
4737
assert { ext (fp_rti m x) = PInf };
4738
ext_le (fp_rti m x) b; absurd
4739
end in
4740
{qed}
4741
if le_diff_zero pinf' t then begin
4742
{proof…}
4743
let lemma pt (x: t) : unit
4744
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz x pinf }
4745
= index_range x; index_range pinf in
4746
{qed}
4747
rti_down fm minf m b pinf 2
4748
end else if le_diff_zero t minf' then begin
4749
{proof…}
4750
let lemma pt (x: t) : unit
4751
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_rti m x) b }
4752
ensures { le_dz x minf }
4753
= reach x;
4754
if pure { not is_infinity x } then begin
4755
index_range t; index_range minf'; assert { ext t = MInf }; absurd
4756
end in
4757
{qed}
4758
minf
4759
end else begin
4760
{proof…}
4761
index_range t; index_range pinf'; index_range minf'; assert { not is_infinity t };
4762
{qed}
4763
let b0 = if is_zero_f fm nz pz b then pz else b in
4764
let ct = convert fr m t in
4765
let strict = not (le_diff_zero ct b0) in
4766
let n = if strict then round_int_v rtp t - 1 else round_int_v rtn t in
4767
{proof…}
4768
convert_not_nan fm m t;
4769
assert { le_dz b b0 /\ fmt b0 = fm /\ is_not_nan b0 };
4770
(* `t` rounds above `b0`: the integers are strictly below `t` *)
4771
let lemma nk (x: t) : unit
4772
requires { fmt x = fm /\ is_not_nan x /\ not is_infinity x /\ le_dz (fp_rti m x) b }
4773
ensures { round_int m (to_real x) <= n }
4774
= reach x;
4775
let ghost r = fin_val x in
4776
let ghost tt = fin_val t in
4777
let ghost k = round_int m r in
4778
if strict then begin
4779
if pure { from_int k = tt } then begin
4780
let ghost zx = if is_positive x then false else true in
4781
let ghost zt = if is_positive t then false else true in
4782
assert { fp_rti m x = round_f fm m zx (ext t) };
4783
assert { ct = round_f fm m zt (ext t) };
4784
round_f_val fm m zx zt (ext t);
4785
total ct b0;
4786
assert { lt_dz b0 ct };
4787
assert { not (is_zero ct /\ is_zero b0) by is_zero b0 -> b0 = pz };
4788
assert { not ele (ext ct) (ext b0) };
4789
ext_le (fp_rti m x) b; ext_le b b0;
4790
absurd
4791
end;
4792
round_int_le RTP tt (round_int RTP tt)
4793
end else round_int_floor tt k in
4794
{qed}
4795
let k = match m with
4796
| RTN -> 2 * n + 2
4797
| RTP -> 2 * n
4798
| RTZ -> if n < 0 then 2 * n else 2 * n + 2
4799
| RNE | RNA -> 2 * n + 1
4800
end in
4801
let c0 = of_q fr rtn false k 2 in
4802
let c = if is_zero_f fm nz pz c0 then pz else c0 in
4803
{proof…}
4804
assert { from_int k /. 2.0 = int_thr_hi m n };
4805
let lemma below (x: t) : unit
4806
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_rti m x) b }
4807
ensures { le_dz x c }
4808
= reach x;
4809
if pure { is_minus_infinity x } then begin
4810
index_range x; index_range c
4811
end else begin
4812
let ghost r = fin_val x in
4813
let ghost tt = fin_val t in
4814
nk x;
4815
round_int_le m r n;
4816
let ghost zx = if is_positive x then false else true in
4817
convert_exact rtn x;
4818
assert { round_f fm rtn zx (ext x) = x };
4819
round_f_ele fm rtn zx false (ext x) (Fin (from_int k /. 2.0));
4820
assert { ele (ext x) (ext c) };
4821
ele_le x c
4822
end in
4823
{qed}
4824
rti_down fm minf m b c 2
4825
end
4826
end
4827
Valid (315ms) (colibri2 17) (cvc4 2) (cvc5 6) (split_vc) (depth 1)
4828
let
rti_back_iv
(ghost fm: format) (minf pinf: t) (m: mode) (a b: t) : dom
4829
requires { fmt minf = fm /\ fmt pinf = fm }
4830
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4831
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b }
4832
ensures { result.fm = fm }
4833
ensures { forall x. fmt x = fm -> is_not_nan x -> in_iv (fp_rti m x) a b -> mem x result }
4834
= let fr = format_of a in
4835
{proof…}
4836
le_not_nan a b; index_range minf; index_range pinf;
4837
let lemma reg (x: t) : unit
4838
requires { fmt x = fm /\ is_not_nan x } ensures { in_iv x minf pinf }
4839
= index_range x in
4840
{qed}
4841
match widen fr with
4842
| None -> interval fm minf pinf
4843
| Some fm' ->
4844
let nz = of_index_v fr (-1) in
4845
let pz = of_index_v fr 0 in
4846
{proof…} of_index_range fm (-1); of_index_range fm 0; {qed}
4847
let lo = rti_lo fm fm' minf pinf nz pz m a in
4848
let hi = rti_hi fm fm' minf pinf nz pz m b in
4849
if lt_diff_zero hi lo then begin
4850
{proof…}
4851
let lemma no (x: t) : unit
4852
requires { fmt x = fm /\ is_not_nan x /\ in_iv (fp_rti m x) a b }
4853
ensures { false }
4854
= le_trans lo x hi; lt_not_le hi lo in
4855
{qed}
4856
empty_dom fm
4857
end else begin
4858
{proof…} total lo hi; {qed}
4859
interval fm lo hi
4860
end
4861
end
4862
Valid (424ms) (cvc5)
4863
let rec
rti_back_u
(ghost fm: format) (minf pinf: t) (m: mode)
4864
(l: list (t, t)) : dom
4865
requires { fmt minf = fm /\ fmt pinf = fm }
4866
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4867
requires { ok_u l /\ fmt_u fm l }
4868
ensures { result.fm = fm }
4869
ensures { forall x. fmt x = fm -> is_not_nan x -> mem_u (fp_rti m x) l -> mem x result }
4870
variant { l }
4871
= match l with
4872
| Nil -> empty_dom fm
4873
| Cons (a, b) r ->
4874
union fm (rti_back_iv fm minf pinf m a b) (rti_back_u fm minf pinf m r)
4875
end

The x such that roundToIntegral x is in r

  Valid (306ms) (cvc5)
4878
let
rti_back
(ghost fm: format) (minf pinf: t) (m: mode) (r: dom) : dom
4879
requires { fmt minf = fm /\ fmt pinf = fm }
4880
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
4881
requires { r.fm = fm }
4882
ensures { result.fm = fm }
4883
ensures { forall x. fmt x = fm -> mem (fp_rti m x) r -> mem x result }
4884
= let u = rti_back_u fm minf pinf m r.u in
4885
{proof…}
4886
let lemma pt (x: t) : unit
4887
requires { fmt x = fm /\ is_nan x } ensures { is_nan (fp_rti m x) }
4888
= () in
4889
let lemma pt2 (x: t) : unit
4890
requires { fmt x = fm /\ is_not_nan x } ensures { is_not_nan (fp_rti m x) }
4891
= rti_not_nan m x in
4892
{qed}
4893
if r.nan then union fm (nan_dom fm) u else u
Backward multiplication

The x such that x * y is in [a, b] for some y in [c, d]. By the sign s of x and the part of [c, d]: on the finite nonzero y of one sign, x * y is monotonic in x and in y (Facts.mul_mag), and the bounds are computed directly (mul_lo_pos, mul_hi_pos) with the end of [c, d] that gives the extreme product; the zeros and the infinities of [c, d] are exact cases.

The floats of sign s: [+0, +inf] or [-inf, -0]

4905
predicate
region
(fm: format) (s: bool) (lo hi: t) =
4906
if s then at_index fm lo 0 /\ hi = plus_infinity fm
4907
else lo = minus_infinity fm /\ at_index fm hi (-1)
4908
Valid (875ms) (colibri2 4) (cvc5 2) (split_vc) (depth 1)
4909
let ghost
region_facts
(fm: format) (s: bool) (lo hi x: t) : unit
4910
requires { region fm s lo hi }
4911
ensures { fmt lo = fm /\ fmt hi = fm /\ le_dz lo hi }
4912
ensures { in_iv x lo hi <-> fmt x = fm /\ is_not_nan x /\ (is_positive x <-> s) }
4913
= {proof…}
4914
index_range lo; index_range hi;
4915
if is_not_nan x && pure { fmt x = fm } then index_range x;
4916
if pure { in_iv x lo hi } then le_not_nan lo x
4917
{qed}

Finite and not zero

4920
predicate
fin_nz
(y: t) = is_not_nan y /\ not is_zero y /\ not is_infinity y
4921
Valid (552ms) (colibri2 18) (cvc4 2) (cvc5 6) (split_vc) (depth 1)
4922
let ghost
mul_mono
(fm: format) (m: mode) (s: bool) (lo hi y: t) : unit
4923
requires { region fm s lo hi /\ fmt y = fm /\ fin_nz y }
4924
ensures { is_positive y -> forall x1 x2. in_iv x1 lo hi -> in_iv x2 lo hi ->
4925
le_dz x1 x2 -> le_dz (fp_mul m x1 y) (fp_mul m x2 y) }
4926
ensures { not is_positive y -> forall x1 x2. in_iv x1 lo hi -> in_iv x2 lo hi ->
4927
le_dz x1 x2 -> le_dz (fp_mul m (neg x1) y) (fp_mul m (neg x2) y) }
4928
= {proof…}
4929
le_refl y;
4930
let lemma nn (x: t) : unit
4931
requires { in_iv x lo hi }
4932
ensures { is_not_nan (fp_mul m x y) /\ fmt (fp_mul m x y) = fm }
4933
ensures { is_not_nan (fp_mul m (neg x) y) /\ fmt (fp_mul m (neg x) y) = fm }
4934
= region_facts fm s lo hi x; mul_special m x y; mul_special m (neg x) y;
4935
mul_div_fmt m x y; mul_div_fmt m (neg x) y in
4936
let lemma mo (x1 x2: t) : unit
4937
requires { in_iv x1 lo hi /\ in_iv x2 lo hi /\ le_dz x1 x2 }
4938
ensures { is_positive y -> le_dz (fp_mul m x1 y) (fp_mul m x2 y) }
4939
ensures { not is_positive y -> le_dz (fp_mul m (neg x1) y) (fp_mul m (neg x2) y) }
4940
= region_facts fm s lo hi x1; region_facts fm s lo hi x2; nn x1; nn x2;
4941
index_range x1; index_range x2;
4942
index_range (neg x1); index_range (neg x2);
4943
if s then begin
4944
assert { mag_le false (neg x1) (neg x2) };
4945
if is_positive y then mul_mag m true true x1 x2 y y
4946
else mul_mag m false false (neg x1) (neg x2) y y
4947
end else begin
4948
assert { mag_le true (neg x2) (neg x1) };
4949
if is_positive y then mul_mag m false true x2 x1 y y
4950
else mul_mag m true false (neg x2) (neg x1) y y
4951
end in
4952
()
4953
{qed}

Direct bounds of the backward multiplication

For y finite and positive, the x with a <= x * y have x * y at or above t = thr_lo a (in the format fm' with one more bit), so x at or above t / y, rounded up in fm' then in fm (both monotonic, and exact on the floats of fm). Then at most two steps up while the bound itself rounds below a (ties, strict thresholds). A zero a (resp. b) keeps its sign: the product has the sign of x. Sound for any mode; the same results as the former bisection (Search) on 300000 random cases of 3 formats and the 5 modes (2026–11–09). A negative y is the positive -y on -x.

Moving a positive factor across an inequality

  Valid (40ms) (colibri2 4) (split_vc) (depth 1)
4968
let ghost
emul_move_ge
(e ex: ereal) (v: real) : unit
4969
requires { 0.0 <. v /\ ele e (emul ex (Fin v)) }
4970
ensures { ele (ediv e (Fin v)) ex }
4971
= {proof…}
4972
assert { einv (Fin v) = Fin (1.0 /. v) };
4973
match e, ex with
4974
| Fin r, Fin w -> assert { r <=. w *. v }; assert { r /. v <=. w }
4975
| _ -> ()
4976
end
4977
{qed}
4978
Valid (838ms) (colibri2 3) (cvc5) (split_vc) (depth 1)
4979
let ghost
emul_move_le
(e ex: ereal) (v: real) : unit
4980
requires { 0.0 <. v /\ ele (emul ex (Fin v)) e }
4981
ensures { ele ex (ediv e (Fin v)) }
4982
= {proof…}
4983
assert { einv (Fin v) = Fin (1.0 /. v) };
4984
match e, ex with
4985
| Fin r, Fin w -> assert { w *. v <=. r }; assert { w <=. r /. v }
4986
| _ -> ()
4987
end
4988
{qed}
4989
Valid (2.8s) (cvc5) (split_vc) (depth 1)
4990
let ghost
emul_eneg
(a b: ereal) : unit
4991
ensures { emul (eneg a) (eneg b) = emul a b }
4992
= {proof…} () {qed}

The product of the opposites

  Valid (290ms) (cvc4)
4995
let ghost
mul_neg_neg
(m: mode) (x y: t) : unit
4996
requires { is_not_nan x /\ is_not_nan y /\ fmt x = fmt y }
4997
ensures { fp_mul m (neg x) (neg y) = fp_mul m x y }
4998
= {proof…}
4999
index_range x; index_range y;
5000
assert { is_zero (neg x) <-> is_zero x };
5001
assert { is_zero (neg y) <-> is_zero y };
5002
assert { is_infinity (neg x) <-> is_infinity x
5003
by is_plus_infinity x <-> index x = nb_sign (fmt x) - 1 };
5004
assert { is_infinity (neg y) <-> is_infinity y
5005
by is_plus_infinity y <-> index y = nb_sign (fmt y) - 1 };
5006
assert { is_positive (neg x) <-> not is_positive x };
5007
assert { is_positive (neg y) <-> not is_positive y };
5008
assert { mul_zneg (neg x) (neg y) = mul_zneg x y };
5009
emul_eneg (ext x) (ext y)
5010
{qed}

The opposite reverses the order

  Valid (79ms) (colibri2)
5013
let ghost
neg_le
(u x: t) : unit
5014
requires { le_dz u x }
5015
ensures { le_dz (neg x) (neg u) }
5016
= {proof…} le_not_nan u x; index_range u; index_range x {qed}
5017
Valid (309ms) (cvc5)
5018
let rec
mul_trim_lo
(ghost fm: format) (pinf: t) (m: mode) (a y l: t) (fuel: int) : t
5019
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
5020
requires { fmt a = fm /\ fmt y = fm /\ fmt l = fm /\ is_not_nan l /\ fin_nz y }
5021
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_mul m x y) -> le_dz l x }
5022
ensures { fmt result = fm /\ is_not_nan result }
5023
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_mul m x y) -> le_dz result x }
5024
variant { fuel }
5025
= if fuel <= 0 || is_pinf fm pinf l then l
5026
else if lt_diff_zero (mul m l y) a then begin
5027
{proof…}
5028
let lemma pt (x: t) : unit
5029
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_mul m x y) }
5030
ensures { le_dz (succ_dz l) x }
5031
= lt_not_le (fp_mul m l y) a;
5032
if pure { x = l } then absurd;
5033
index_range x; index_range l;
5034
assert { lt_dz l x };
5035
succ_least l x in
5036
index_range l; of_index_range fm (index l + 1);
5037
{qed}
5038
mul_trim_lo fm pinf m a y (succ_diff_zero l) (fuel - 1)
5039
end else l
5040
Valid (269ms) (cvc5)
5041
let rec
mul_trim_hi
(ghost fm: format) (minf: t) (m: mode) (b y h: t) (fuel: int) : t
5042
requires { fmt minf = fm /\ minf = minus_infinity fm }
5043
requires { fmt b = fm /\ fmt y = fm /\ fmt h = fm /\ is_not_nan h /\ fin_nz y }
5044
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_mul m x y) b -> le_dz x h }
5045
ensures { fmt result = fm /\ is_not_nan result }
5046
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_mul m x y) b -> le_dz x result }
5047
variant { fuel }
5048
= if fuel <= 0 || is_minf fm minf h then h
5049
else if lt_diff_zero b (mul m h y) then begin
5050
{proof…}
5051
let lemma pt (x: t) : unit
5052
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_mul m x y) b }
5053
ensures { le_dz x (pred_dz h) }
5054
= lt_not_le b (fp_mul m h y);
5055
if pure { x = h } then absurd;
5056
index_range x; index_range h;
5057
assert { lt_dz x h };
5058
pred_greatest h x in
5059
index_range h; of_index_range fm (index h - 1);
5060
{qed}
5061
mul_trim_hi fm minf m b y (pred_diff_zero h) (fuel - 1)
5062
end else h

A lower bound of the x with a <= x * y, y finite and positive

  Valid (823ms) (colibri2 37) (cvc4 5) (cvc5 4) (split_vc) (depth 1)
5065
let
mul_lo_pos
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (a y: t) : t
5066
requires { widens fm fm' /\ fmt a = fm /\ fmt y = fm }
5067
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5068
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
5069
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
5070
requires { is_not_nan a /\ fin_nz y /\ is_positive y }
5071
ensures { fmt result = fm /\ is_not_nan result }
5072
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_mul m x y) -> le_dz result x }
5073
= if is_minf fm minf a then begin
5074
{proof…}
5075
let lemma pt (x: t) : unit
5076
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz minf x }
5077
= index_range x; index_range minf in
5078
{qed}
5079
minf
5080
end else if is_pz fm pz a && lt_diff_zero (mul m nz y) a then begin
5081
(* the product keeps the sign of `x`: only the positive `x` reach +0 *)
5082
{proof…}
5083
mul_mono fm m false minf nz y;
5084
let lemma pt (x: t) : unit
5085
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_mul m x y) } ensures { le_dz pz x }
5086
= index_range x; index_range nz; index_range minf;
5087
if pure { index x < 0 } then begin
5088
assert { in_iv x minf nz /\ in_iv nz minf nz };
5089
assert { le_dz (fp_mul m x y) (fp_mul m nz y) };
5090
le_trans a (fp_mul m x y) (fp_mul m nz y);
5091
lt_not_le (fp_mul m nz y) a; absurd
5092
end in
5093
{qed}
5094
pz
5095
end else begin
5096
let fr = format_of a in
5097
let t = thr_lo fm fm' nz pz m a in
5098
let y' = convert fm' rtn y in
5099
{proof…}
5100
convert_widen fm' rtn y;
5101
let ghost v = fin_val y in
5102
assert { 0.0 <. v by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
5103
assert { ext y' = Fin v /\ fmt y' = fm' /\ fin_nz y' };
5104
{qed}
5105
let q = div rtp t y' in
5106
let l0 = convert fr rtp q in
5107
{proof…} convert_not_nan fm rtp q; {qed}
5108
let l = if is_zero_f fm nz pz l0 then nz else l0 in
5109
{proof…}
5110
let lemma pt (x: t) : unit
5111
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_mul m x y) }
5112
ensures { le_dz l x }
5113
= assert { fp_mul m x y = round_f fm m (mul_zneg x y) (emul (ext x) (Fin v)) };
5114
assert { ele (ext t) (emul (ext x) (Fin v)) };
5115
emul_move_ge (ext t) (ext x) v;
5116
let ghost zx = if is_positive x then false else true in
5117
convert_widen fm' rtp x;
5118
assert { fp_convert fm' rtp x = round_f fm' rtp zx (ext x) };
5119
round_f_ele fm' rtp (mul_zneg t y') zx (ediv (ext t) (Fin v)) (ext x);
5120
assert { ele (ext q) (ext x) };
5121
let ghost zq = if is_positive q then false else true in
5122
round_f_ele fm rtp zq zx (ext q) (ext x);
5123
assert { round_f fm rtp zx (ext x) = x };
5124
assert { ele (ext l) (ext x) };
5125
ele_le l x in
5126
{qed}
5127
mul_trim_lo fm pinf m a y l 2
5128
end

An upper bound of the x with x * y <= b, y finite and positive

  Valid (801ms) (colibri2 33) (cvc4 10) (cvc5 3) (split_vc) (depth 1)
5131
let
mul_hi_pos
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (b y: t) : t
5132
requires { widens fm fm' /\ fmt b = fm /\ fmt y = fm }
5133
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5134
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
5135
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
5136
requires { is_not_nan b /\ fin_nz y /\ is_positive y }
5137
ensures { fmt result = fm /\ is_not_nan result }
5138
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_mul m x y) b -> le_dz x result }
5139
= if is_pinf fm pinf b then begin
5140
{proof…}
5141
let lemma pt (x: t) : unit
5142
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz x pinf }
5143
= index_range x; index_range pinf in
5144
{qed}
5145
pinf
5146
end else if is_nz fm nz b && lt_diff_zero b (mul m pz y) then begin
5147
(* only the negative `x` reach -0 or below *)
5148
{proof…}
5149
mul_mono fm m true pz pinf y;
5150
let lemma pt (x: t) : unit
5151
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_mul m x y) b } ensures { le_dz x nz }
5152
= index_range x; index_range pz; index_range pinf;
5153
if pure { 0 <= index x } then begin
5154
assert { in_iv x pz pinf /\ in_iv pz pz pinf };
5155
assert { le_dz (fp_mul m pz y) (fp_mul m x y) };
5156
le_trans (fp_mul m pz y) (fp_mul m x y) b;
5157
lt_not_le b (fp_mul m pz y); absurd
5158
end in
5159
{qed}
5160
nz
5161
end else begin
5162
let fr = format_of b in
5163
let t = thr_hi fm fm' nz pz m b in
5164
let y' = convert fm' rtn y in
5165
{proof…}
5166
convert_widen fm' rtn y;
5167
let ghost v = fin_val y in
5168
assert { 0.0 <. v by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
5169
assert { ext y' = Fin v /\ fmt y' = fm' /\ fin_nz y' };
5170
{qed}
5171
let q = div rtn t y' in
5172
let h0 = convert fr rtn q in
5173
{proof…} convert_not_nan fm rtn q; {qed}
5174
let h = if is_zero_f fm nz pz h0 then pz else h0 in
5175
{proof…}
5176
let lemma pt (x: t) : unit
5177
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_mul m x y) b }
5178
ensures { le_dz x h }
5179
= assert { fp_mul m x y = round_f fm m (mul_zneg x y) (emul (ext x) (Fin v)) };
5180
assert { ele (emul (ext x) (Fin v)) (ext t) };
5181
emul_move_le (ext t) (ext x) v;
5182
let ghost zx = if is_positive x then false else true in
5183
convert_widen fm' rtn x;
5184
assert { fp_convert fm' rtn x = round_f fm' rtn zx (ext x) };
5185
round_f_ele fm' rtn zx (mul_zneg t y') (ext x) (ediv (ext t) (Fin v));
5186
assert { ele (ext x) (ext q) };
5187
let ghost zq = if is_positive q then false else true in
5188
round_f_ele fm rtn zx zq (ext x) (ext q);
5189
assert { round_f fm rtn zx (ext x) = x };
5190
assert { ele (ext x) (ext h) };
5191
ele_le x h in
5192
{qed}
5193
mul_trim_hi fm minf m b y h 2
5194
end

The x of sign s with x * y in [a, b] for some y of [c, d], finite, not zero and of one sign. lo, hi are the floats of sign s, lo', hi' the ones of the other sign. The bounds come from the end d (resp. c) for a positive x, c (resp. d) for a negative one. For a negative y, the bounds of -x by -y.

  Valid (1.1s) (colibri2 76) (cvc4 5) (cvc5 19) (split_vc 2) (depth 2)
5201
let
mul_back_fin
(ghost fm: format) (m: mode) (s: bool) (lo hi lo' hi': t)
5202
(q: bool) (a b c d: t) : dom
5203
requires { region fm s lo hi /\ region fm (not s) lo' hi' }
5204
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
5205
requires { le_dz a b /\ le_dz c d /\ fin_nz c /\ fin_nz d }
5206
requires { (is_positive c <-> q) /\ (is_positive d <-> q) }
5207
ensures { result.fm = fm }
5208
ensures { forall x y. in_iv x lo hi -> in_iv y c d ->
5209
in_iv (fp_mul m x y) a b -> mem x result }
5210
= {proof…}
5211
le_not_nan a b; le_not_nan c d;
5212
region_facts fm s lo hi lo; region_facts fm (not s) lo' hi' lo;
5213
mul_mono fm m s lo hi c; mul_mono fm m s lo hi d;
5214
mul_mono fm m (not s) lo' hi' c; mul_mono fm m (not s) lo' hi' d;
5215
let lemma yfacts (y: t) : unit
5216
requires { in_iv y c d }
5217
ensures { fin_nz y /\ fmt y = fm /\ (is_positive y <-> is_positive c) }
5218
= le_not_nan c y; index_range c; index_range d; index_range y in
5219
{qed}
5220
let ya = if s then d else c in
5221
let yb = if s then c else d in
5222
{proof…}
5223
(* the extreme products *)
5224
let lemma ext (x y: t) : unit
5225
requires { in_iv x lo hi /\ in_iv y c d }
5226
ensures { le_dz (fp_mul m x y) (fp_mul m x ya) }
5227
ensures { le_dz (fp_mul m x yb) (fp_mul m x y) }
5228
= yfacts y; region_facts fm s lo hi x;
5229
le_refl x; mul_special m x y; mul_special m x c; mul_special m x d;
5230
if is_positive c then begin
5231
if s then begin
5232
mul_mag m true true x x y d; mul_mag m true true x x c y
5233
end else begin
5234
mul_mag m false true x x c y; mul_mag m false true x x y d
5235
end
5236
end else begin
5237
assert { mag_le false y c /\ mag_le false d y };
5238
if s then begin
5239
mul_mag m true false x x d y; mul_mag m true false x x y c
5240
end else begin
5241
mul_mag m false false x x y c; mul_mag m false false x x d y
5242
end
5243
end in
5244
{qed}
5245
{proof…}
5246
region_facts fm s lo hi lo; region_facts fm s lo hi hi;
5247
region_facts fm (not s) lo' hi' lo';
5248
index_range lo; index_range hi; index_range lo'; index_range hi';
5249
{qed}
5250
let fr = format_of a in
5251
let minf = if s then lo' else lo in
5252
let pinf = if s then hi else hi' in
5253
let nz = if s then hi' else hi in
5254
let pz = if s then lo else lo' in
5255
{proof…}
5256
assert { minf = minus_infinity fm /\ pinf = plus_infinity fm };
5257
assert { at_index fm nz (-1) /\ at_index fm pz 0 };
5258
{qed}
5259
match widen fr with
5260
| None -> interval fm lo hi
5261
| Some fm' ->
5262
let (l0, h0) =
5263
if q then
5264
(mul_lo_pos fm fm' minf pinf nz pz m a ya, mul_hi_pos fm fm' minf pinf nz pz m b yb)
5265
else begin
5266
{proof…}
5267
yfacts c; yfacts d; le_refl c; le_refl d;
5268
assert { fin_nz (neg ya) /\ is_positive (neg ya) /\ fmt (neg ya) = fm };
5269
assert { fin_nz (neg yb) /\ is_positive (neg yb) /\ fmt (neg yb) = fm };
5270
{qed}
5271
(neg (mul_hi_pos fm fm' minf pinf nz pz m b (neg yb)),
5272
neg (mul_lo_pos fm fm' minf pinf nz pz m a (neg ya)))
5273
end in
5274
let l = max_diff_zero l0 lo in
5275
let h = min_diff_zero h0 hi in
5276
{proof…}
5277
let lemma pt (x y: t) : unit
5278
requires { in_iv x lo hi /\ in_iv y c d /\ in_iv (fp_mul m x y) a b }
5279
ensures { le_dz l x /\ le_dz x h }
5280
= ext x y; le_trans a (fp_mul m x y) (fp_mul m x ya);
5281
le_trans (fp_mul m x yb) (fp_mul m x y) b;
5282
region_facts fm s lo hi x;
5283
if not q then begin
5284
yfacts c; yfacts d; le_refl c; le_refl d;
5285
mul_neg_neg m x ya; mul_neg_neg m x yb;
5286
index_range x; index_range (neg x); neg_neg x;
5287
assert { le_dz (neg h0) (neg x) /\ le_dz (neg x) (neg l0) };
5288
neg_le (neg h0) (neg x); neg_le (neg x) (neg l0);
5289
neg_neg h0; neg_neg l0
5290
end in
5291
{qed}
5292
if lt_diff_zero h l then begin
5293
{proof…}
5294
let lemma no (x y: t) : unit
5295
requires { in_iv x lo hi /\ in_iv y c d /\ in_iv (fp_mul m x y) a b }
5296
ensures { false }
5297
= pt x y; le_trans l x h; lt_not_le h l in
5298
{qed}
5299
empty_dom fm
5300
end else begin
5301
{proof…} total l h; {qed}
5302
interval fm l h
5303
end
5304
end
5305
Valid (22ms) (colibri2)
5306
let
in_ivf
(v a b: t) : bool ensures { result <-> in_iv v a b }
5307
= le_diff_zero a v && le_diff_zero v b

[a, b] if c, else empty

  Valid (23ms) (colibri2)
5310
let
iv_if
(ghost fm: format) (c: bool) (a b: t) : dom
5311
requires { fmt a = fm /\ fmt b = fm /\ is_not_nan a /\ is_not_nan b /\ le_dz a b }
5312
ensures { result.fm = fm }
5313
ensures { forall v. c -> is_not_nan v -> in_iv v a b -> mem v result }
5314
= if c then interval fm a b else empty_dom fm

The x with x * y in [a, b] for some y of [c, d]

  Valid (3.7s) (colibri2 73) (cvc4 18) (cvc5 52) (split_vc 57) (depth 5)
5317
let
mul_back_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b c d: t) : dom
5318
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5319
requires { specials4 fm minf pinf nz pz }
5320
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
5321
requires { le_dz a b /\ le_dz c d }
5322
ensures { result.fm = fm }
5323
ensures { forall x y. fmt x = fm -> is_not_nan x -> in_iv y c d ->
5324
in_iv (fp_mul m x y) a b -> mem x result }
5325
= {proof…}
5326
le_not_nan a b; le_not_nan c d; format_facts fm ();
5327
index_range minf; index_range pinf; index_range nz; index_range pz;
5328
of_index_range fm 1; of_index_range fm (-2);
5329
of_index_range fm (nb_sign fm - 2); of_index_range fm (1 - nb_sign fm);
5330
succ_gt pz; pred_lt pinf; succ_gt minf; pred_lt nz;
5331
{qed}
5332
let psub = succ_diff_zero pz in
5333
let pmax = pred_diff_zero pinf in
5334
let nmin = succ_diff_zero minf in
5335
let nmax = pred_diff_zero nz in
5336
{proof…}
5337
assert { index psub = 1 /\ index pmax = nb_sign fm - 2 };
5338
assert { index nmin = 1 - nb_sign fm /\ index nmax = -2 };
5339
region_facts fm true pz pinf pz; region_facts fm false minf nz pz;
5340
{qed}
5341
(* the finite positive y *)
5342
let c1 = max_diff_zero c psub in
5343
let d1 = min_diff_zero d pmax in
5344
let fp =
5345
if le_diff_zero c1 d1 then begin
5346
{proof…} index_range c1; index_range d1; {qed}
5347
union fm (mul_back_fin fm m true pz pinf minf nz true a b c1 d1)
5348
(mul_back_fin fm m false minf nz pz pinf true a b c1 d1)
5349
end else empty_dom fm in
5350
(* the finite negative y *)
5351
let c2 = max_diff_zero c nmin in
5352
let d2 = min_diff_zero d nmax in
5353
let fn =
5354
if le_diff_zero c2 d2 then begin
5355
{proof…} index_range c2; index_range d2; {qed}
5356
union fm (mul_back_fin fm m true pz pinf minf nz false a b c2 d2)
5357
(mul_back_fin fm m false minf nz pz pinf false a b c2 d2)
5358
end else empty_dom fm in
5359
(* the zeros: a finite x gives a zero *)
5360
let zn = in_ivf nz a b in
5361
let zp = in_ivf pz a b in
5362
let z1 = if in_ivf nz c d then union fm (iv_if fm zn pz pmax) (iv_if fm zp nmin nz)
5363
else empty_dom fm in
5364
let z2 = if in_ivf pz c d then union fm (iv_if fm zp pz pmax) (iv_if fm zn nmin nz)
5365
else empty_dom fm in
5366
(* the infinities: a nonzero x gives an infinity *)
5367
let im = in_ivf minf a b in
5368
let ip = in_ivf pinf a b in
5369
let i1 = if in_ivf minf c d then union fm (iv_if fm im psub pinf) (iv_if fm ip minf nmax)
5370
else empty_dom fm in
5371
let i2 = if in_ivf pinf c d then union fm (iv_if fm ip psub pinf) (iv_if fm im minf nmax)
5372
else empty_dom fm in
5373
let res = union fm (union fm fp fn) (union fm (union fm z1 z2) (union fm i1 i2)) in
5374
{proof…}
5375
let lemma pt (x y: t) : unit
5376
requires { fmt x = fm /\ is_not_nan x /\ in_iv y c d /\ in_iv (fp_mul m x y) a b }
5377
ensures { mem x res }
5378
= le_not_nan c y; le_not_nan a (fp_mul m x y);
5379
index_range x; index_range y;
5380
region_facts fm true pz pinf x; region_facts fm false minf nz x;
5381
mul_special m x y;
5382
if pure { is_zero y } then begin
5383
if pure { is_positive y } then
5384
assert { y = pz by of_index fm (index y) = of_index fm (index pz) }
5385
else
5386
assert { y = nz by of_index fm (index y) = of_index fm (index nz) };
5387
if pure { is_zero (fp_mul m x y) } then begin
5388
let r = fp_mul m x y in
5389
index_range r;
5390
if pure { is_positive r } then
5391
assert { r = pz by of_index fm (index r) = of_index fm (index pz) }
5392
else
5393
assert { r = nz by of_index fm (index r) = of_index fm (index nz) }
5394
end;
5395
assert { mem x z1 \/ mem x z2 }
5396
end else if pure { is_infinity y } then begin
5397
if pure { is_plus_infinity y } then plus_infinity_unique y
5398
else minus_infinity_unique y;
5399
assert { mem x i1 \/ mem x i2 }
5400
end else if pure { is_positive y } then begin
5401
assert { in_iv y c1 d1 };
5402
assert { mem x fp }
5403
end else begin
5404
assert { in_iv y c2 d2 };
5405
assert { mem x fn }
5406
end in
5407
{qed}
5408
res
5409
Valid (473ms) (cvc5)
5410
let rec
mul_back_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b: t)
5411
(l: list (t, t)) : dom
5412
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5413
requires { specials4 fm minf pinf nz pz }
5414
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l /\ fmt_u fm l }
5415
ensures { result.fm = fm }
5416
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l ->
5417
in_iv (fp_mul m x y) a b -> mem x result }
5418
variant { l }
5419
= match l with
5420
| Nil -> empty_dom fm
5421
| Cons (c, d) r ->
5422
union fm (mul_back_iv fm minf pinf nz pz m a b c d)
5423
(mul_back_iv_u fm minf pinf nz pz m a b r)
5424
end
5425
Valid (506ms) (cvc5)
5426
let rec
mul_back_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
5427
(l1 l2: list (t, t)) : dom
5428
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5429
requires { specials4 fm minf pinf nz pz }
5430
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
5431
ensures { result.fm = fm }
5432
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l2 ->
5433
mem_u (fp_mul m x y) l1 -> mem x result }
5434
variant { l1 }
5435
= match l1 with
5436
| Nil -> empty_dom fm
5437
| Cons (a, b) r ->
5438
union fm (mul_back_iv_u fm minf pinf nz pz m a b l2)
5439
(mul_back_u fm minf pinf nz pz m r l2)
5440
end

The x such that x * y is in r for some y in d; by commutativity (Facts.mul_comm), also the y such that x * y is in r for some x in d

  Valid (568ms) (colibri2 17) (cvc4 7) (cvc5 16) (split_vc) (depth 1)
5445
let
mul_back
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r d: dom) : dom
5446
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5447
requires { specials4 fm minf pinf nz pz }
5448
requires { r.fm = fm /\ d.fm = fm }
5449
ensures { result.fm = fm }
5450
ensures { forall x y. fmt x = fm -> mem y d -> mem (fp_mul m x y) r -> mem x result }
5451
ensures { forall x y. fmt y = fm -> mem x d -> mem (fp_mul m x y) r -> mem y result }
5452
= {proof…}
5453
index_range minf; index_range pinf; index_range nz; index_range pz;
5454
{qed}
5455
let u = mul_back_u fm minf pinf nz pz m r.u d.u in
5456
let n =
5457
if r.nan then
5458
if d.nan then top fm minf pinf
5459
else begin
5460
let p = if meets fm minf minf d || meets fm pinf pinf d
5461
then union fm (singleton fm nz) (singleton fm pz) else empty_dom fm in
5462
let q = if meets fm nz pz d
5463
then union fm (singleton fm minf) (singleton fm pinf) else empty_dom fm in
5464
union fm (nan_dom fm) (union fm p q)
5465
end
5466
else empty_dom fm in
5467
let res = union fm n u in
5468
{proof…}
5469
let lemma pt (x y: t) : unit
5470
requires { fmt x = fm /\ mem y d /\ mem (fp_mul m x y) r }
5471
ensures { mem x res }
5472
= mul_special m x y; mul_div_fmt m x y;
5473
if pure { is_nan (fp_mul m x y) } then begin
5474
if pure { is_not_nan x /\ is_not_nan y } then begin
5475
index_range x; index_range y;
5476
if pure { is_zero x } then begin
5477
if pure { is_positive x } then
5478
assert { x = pz by of_index fm (index x) = of_index fm (index pz) }
5479
else
5480
assert { x = nz by of_index fm (index x) = of_index fm (index nz) };
5481
if pure { is_plus_infinity y } then plus_infinity_unique y
5482
else minus_infinity_unique y
5483
end else begin
5484
if pure { is_plus_infinity x } then plus_infinity_unique x
5485
else minus_infinity_unique x;
5486
assert { index nz <= index y <= index pz }
5487
end
5488
end
5489
end in
5490
let lemma pt2 (x y: t) : unit
5491
requires { fmt y = fm /\ mem x d /\ mem (fp_mul m x y) r }
5492
ensures { mem y res }
5493
= mul_comm m x y; pt y x in
5494
{qed}
5495
res
Backward division: the dividend

The x such that x / y is in [a, b] for some y in [c, d], as for the product (Facts.div_mag); the zeros and infinities of y exchange their roles.

Direct bounds of the backward division: the dividend

As for the product: for y finite and positive, a <= x / y gives x / y at or above t = thr_lo a in fm', so x at or above t * y, rounded up in fm' then in fm; then at most two steps. A negative y is the positive -y on -x.

Moving a positive divisor across an inequality

  Valid (48ms) (colibri2 4) (split_vc) (depth 1)
5511
let ghost
ediv_move_ge
(e ex: ereal) (v: real) : unit
5512
requires { 0.0 <. v /\ ele e (ediv ex (Fin v)) }
5513
ensures { ele (emul e (Fin v)) ex }
5514
= {proof…}
5515
assert { einv (Fin v) = Fin (1.0 /. v) /\ 0.0 <. 1.0 /. v };
5516
match e, ex with
5517
| Fin r, Fin w -> assert { r <=. w *. (1.0 /. v) }; assert { r *. v <=. w }
5518
| _ -> ()
5519
end
5520
{qed}
5521
Valid (51ms) (colibri2)
5522
let ghost
ediv_move_le
(e ex: ereal) (v: real) : unit
5523
requires { 0.0 <. v /\ ele (ediv ex (Fin v)) e }
5524
ensures { ele ex (emul e (Fin v)) }
5525
= {proof…}
5526
assert { einv (Fin v) = Fin (1.0 /. v) /\ 0.0 <. 1.0 /. v };
5527
match e, ex with
5528
| Fin r, Fin w -> assert { w *. (1.0 /. v) <=. r }; assert { w <=. r *. v }
5529
| _ -> ()
5530
end
5531
{qed}

The quotient of the opposites

  Valid (412ms) (colibri2 18) (cvc4) (cvc5) (split_vc) (depth 1)
5534
let ghost
div_neg_neg
(m: mode) (x y: t) : unit
5535
requires { is_not_nan x /\ is_not_nan y /\ fmt x = fmt y }
5536
ensures { fp_div m (neg x) (neg y) = fp_div m x y }
5537
= {proof…}
5538
index_range x; index_range y;
5539
assert { is_zero (neg x) <-> is_zero x };
5540
assert { is_zero (neg y) <-> is_zero y };
5541
assert { is_infinity (neg x) <-> is_infinity x
5542
by is_plus_infinity x <-> index x = nb_sign (fmt x) - 1 };
5543
assert { is_infinity (neg y) <-> is_infinity y
5544
by is_plus_infinity y <-> index y = nb_sign (fmt y) - 1 };
5545
assert { is_positive (neg x) <-> not is_positive x };
5546
assert { is_positive (neg y) <-> not is_positive y };
5547
assert { mul_zneg (neg x) (neg y) = mul_zneg x y };
5548
if pure { not is_zero y } then begin
5549
match ext y with
5550
| Fin v ->
5551
assert { v <> 0.0 };
5552
let ghost iv = 1.0 /. v in
5553
let ghost jv = 1.0 /. (-. v) in
5554
assert { v *. iv = 1.0 };
5555
assert { (-. v) *. (-. iv) = 1.0 };
5556
assert { jv *. (-. v) = 1.0 };
5557
assert { jv = jv *. ((-. v) *. (-. iv)) };
5558
assert { jv *. ((-. v) *. (-. iv)) = (jv *. (-. v)) *. (-. iv) };
5559
assert { 1.0 /. (-. v) = -. (1.0 /. v) }
5560
| _ -> ()
5561
end;
5562
assert { einv (ext (neg y)) = eneg (einv (ext y)) };
5563
emul_eneg (ext x) (einv (ext y))
5564
end
5565
{qed}
5566
Valid (244ms) (colibri2 18) (cvc4) (cvc5 7) (split_vc) (depth 1)
5567
let ghost
div_num_mono
(fm: format) (m: mode) (s: bool) (lo hi y: t) : unit
5568
requires { region fm s lo hi /\ fmt y = fm /\ fin_nz y }
5569
ensures { is_positive y -> forall x1 x2. in_iv x1 lo hi -> in_iv x2 lo hi ->
5570
le_dz x1 x2 -> le_dz (fp_div m x1 y) (fp_div m x2 y) }
5571
ensures { not is_positive y -> forall x1 x2. in_iv x1 lo hi -> in_iv x2 lo hi ->
5572
le_dz x1 x2 -> le_dz (fp_div m (neg x1) y) (fp_div m (neg x2) y) }
5573
= {proof…}
5574
le_refl y;
5575
let lemma nn (x: t) : unit
5576
requires { in_iv x lo hi }
5577
ensures { is_not_nan (fp_div m x y) /\ fmt (fp_div m x y) = fm }
5578
ensures { is_not_nan (fp_div m (neg x) y) /\ fmt (fp_div m (neg x) y) = fm }
5579
= region_facts fm s lo hi x; div_special m x y; div_special m (neg x) y;
5580
mul_div_fmt m x y; mul_div_fmt m (neg x) y in
5581
let lemma mo (x1 x2: t) : unit
5582
requires { in_iv x1 lo hi /\ in_iv x2 lo hi /\ le_dz x1 x2 }
5583
ensures { is_positive y -> le_dz (fp_div m x1 y) (fp_div m x2 y) }
5584
ensures { not is_positive y -> le_dz (fp_div m (neg x1) y) (fp_div m (neg x2) y) }
5585
= region_facts fm s lo hi x1; region_facts fm s lo hi x2; nn x1; nn x2;
5586
index_range x1; index_range x2;
5587
index_range (neg x1); index_range (neg x2);
5588
if s then begin
5589
assert { mag_le false (neg x1) (neg x2) };
5590
if is_positive y then div_mag m true true x1 x2 y y
5591
else div_mag m false false (neg x1) (neg x2) y y
5592
end else begin
5593
assert { mag_le true (neg x2) (neg x1) };
5594
if is_positive y then div_mag m false true x2 x1 y y
5595
else div_mag m true false (neg x2) (neg x1) y y
5596
end in
5597
()
5598
{qed}
5599
Valid (714ms) (cvc5)
5600
let rec
div_trim_lo
(ghost fm: format) (pinf: t) (m: mode) (a y l: t) (fuel: int) : t
5601
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
5602
requires { fmt a = fm /\ fmt y = fm /\ fmt l = fm /\ is_not_nan l /\ fin_nz y }
5603
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_div m x y) -> le_dz l x }
5604
ensures { fmt result = fm /\ is_not_nan result }
5605
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_div m x y) -> le_dz result x }
5606
variant { fuel }
5607
= if fuel <= 0 || is_pinf fm pinf l then l
5608
else if lt_diff_zero (div m l y) a then begin
5609
{proof…}
5610
let lemma pt (x: t) : unit
5611
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_div m x y) }
5612
ensures { le_dz (succ_dz l) x }
5613
= lt_not_le (fp_div m l y) a;
5614
if pure { x = l } then absurd;
5615
index_range x; index_range l;
5616
assert { lt_dz l x };
5617
succ_least l x in
5618
index_range l; of_index_range fm (index l + 1);
5619
{qed}
5620
div_trim_lo fm pinf m a y (succ_diff_zero l) (fuel - 1)
5621
end else l
5622
Valid (740ms) (cvc5)
5623
let rec
div_trim_hi
(ghost fm: format) (minf: t) (m: mode) (b y h: t) (fuel: int) : t
5624
requires { fmt minf = fm /\ minf = minus_infinity fm }
5625
requires { fmt b = fm /\ fmt y = fm /\ fmt h = fm /\ is_not_nan h /\ fin_nz y }
5626
requires { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_div m x y) b -> le_dz x h }
5627
ensures { fmt result = fm /\ is_not_nan result }
5628
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_div m x y) b -> le_dz x result }
5629
variant { fuel }
5630
= if fuel <= 0 || is_minf fm minf h then h
5631
else if lt_diff_zero b (div m h y) then begin
5632
{proof…}
5633
let lemma pt (x: t) : unit
5634
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_div m x y) b }
5635
ensures { le_dz x (pred_dz h) }
5636
= lt_not_le b (fp_div m h y);
5637
if pure { x = h } then absurd;
5638
index_range x; index_range h;
5639
assert { lt_dz x h };
5640
pred_greatest h x in
5641
index_range h; of_index_range fm (index h - 1);
5642
{qed}
5643
div_trim_hi fm minf m b y (pred_diff_zero h) (fuel - 1)
5644
end else h

A lower bound of the x with a <= x / y, y finite and positive

  Valid (843ms) (colibri2 37) (cvc4 4) (cvc5 5) (split_vc) (depth 1)
5647
let
div_lo_pos
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (a y: t) : t
5648
requires { widens fm fm' /\ fmt a = fm /\ fmt y = fm }
5649
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5650
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
5651
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
5652
requires { is_not_nan a /\ fin_nz y /\ is_positive y }
5653
ensures { fmt result = fm /\ is_not_nan result }
5654
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz a (fp_div m x y) -> le_dz result x }
5655
= if is_minf fm minf a then begin
5656
{proof…}
5657
let lemma pt (x: t) : unit
5658
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz minf x }
5659
= index_range x; index_range minf in
5660
{qed}
5661
minf
5662
end else if is_pz fm pz a && lt_diff_zero (div m nz y) a then begin
5663
(* the quotient keeps the sign of `x`: only the positive `x` reach +0 *)
5664
{proof…}
5665
div_num_mono fm m false minf nz y;
5666
let lemma pt (x: t) : unit
5667
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_div m x y) } ensures { le_dz pz x }
5668
= index_range x; index_range nz; index_range minf;
5669
if pure { index x < 0 } then begin
5670
assert { in_iv x minf nz /\ in_iv nz minf nz };
5671
assert { le_dz (fp_div m x y) (fp_div m nz y) };
5672
le_trans a (fp_div m x y) (fp_div m nz y);
5673
lt_not_le (fp_div m nz y) a; absurd
5674
end in
5675
{qed}
5676
pz
5677
end else begin
5678
let fr = format_of a in
5679
let t = thr_lo fm fm' nz pz m a in
5680
let y' = convert fm' rtn y in
5681
{proof…}
5682
convert_widen fm' rtn y;
5683
let ghost v = fin_val y in
5684
assert { 0.0 <. v by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
5685
assert { ext y' = Fin v /\ fmt y' = fm' /\ fin_nz y' };
5686
{qed}
5687
let q = mul rtp t y' in
5688
let l0 = convert fr rtp q in
5689
{proof…} convert_not_nan fm rtp q; {qed}
5690
let l = if is_zero_f fm nz pz l0 then nz else l0 in
5691
{proof…}
5692
let lemma pt (x: t) : unit
5693
requires { fmt x = fm /\ is_not_nan x /\ le_dz a (fp_div m x y) }
5694
ensures { le_dz l x }
5695
= assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (ext x) (Fin v)) };
5696
assert { ele (ext t) (ediv (ext x) (Fin v)) };
5697
ediv_move_ge (ext t) (ext x) v;
5698
let ghost zx = if is_positive x then false else true in
5699
convert_widen fm' rtp x;
5700
assert { fp_convert fm' rtp x = round_f fm' rtp zx (ext x) };
5701
round_f_ele fm' rtp (mul_zneg t y') zx (emul (ext t) (Fin v)) (ext x);
5702
assert { ele (ext q) (ext x) };
5703
let ghost zq = if is_positive q then false else true in
5704
round_f_ele fm rtp zq zx (ext q) (ext x);
5705
assert { round_f fm rtp zx (ext x) = x };
5706
assert { ele (ext l) (ext x) };
5707
ele_le l x in
5708
{qed}
5709
div_trim_lo fm pinf m a y l 2
5710
end

An upper bound of the x with x / y <= b, y finite and positive

  Valid (809ms) (colibri2 37) (cvc4 5) (cvc5 4) (split_vc) (depth 1)
5713
let
div_hi_pos
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (b y: t) : t
5714
requires { widens fm fm' /\ fmt b = fm /\ fmt y = fm }
5715
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5716
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
5717
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
5718
requires { is_not_nan b /\ fin_nz y /\ is_positive y }
5719
ensures { fmt result = fm /\ is_not_nan result }
5720
ensures { forall x. fmt x = fm -> is_not_nan x -> le_dz (fp_div m x y) b -> le_dz x result }
5721
= if is_pinf fm pinf b then begin
5722
{proof…}
5723
let lemma pt (x: t) : unit
5724
requires { fmt x = fm /\ is_not_nan x } ensures { le_dz x pinf }
5725
= index_range x; index_range pinf in
5726
{qed}
5727
pinf
5728
end else if is_nz fm nz b && lt_diff_zero b (div m pz y) then begin
5729
(* only the negative `x` reach -0 or below *)
5730
{proof…}
5731
div_num_mono fm m true pz pinf y;
5732
let lemma pt (x: t) : unit
5733
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_div m x y) b } ensures { le_dz x nz }
5734
= index_range x; index_range pz; index_range pinf;
5735
if pure { 0 <= index x } then begin
5736
assert { in_iv x pz pinf /\ in_iv pz pz pinf };
5737
assert { le_dz (fp_div m pz y) (fp_div m x y) };
5738
le_trans (fp_div m pz y) (fp_div m x y) b;
5739
lt_not_le b (fp_div m pz y); absurd
5740
end in
5741
{qed}
5742
nz
5743
end else begin
5744
let fr = format_of b in
5745
let t = thr_hi fm fm' nz pz m b in
5746
let y' = convert fm' rtn y in
5747
{proof…}
5748
convert_widen fm' rtn y;
5749
let ghost v = fin_val y in
5750
assert { 0.0 <. v by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
5751
assert { ext y' = Fin v /\ fmt y' = fm' /\ fin_nz y' };
5752
{qed}
5753
let q = mul rtn t y' in
5754
let h0 = convert fr rtn q in
5755
{proof…} convert_not_nan fm rtn q; {qed}
5756
let h = if is_zero_f fm nz pz h0 then pz else h0 in
5757
{proof…}
5758
let lemma pt (x: t) : unit
5759
requires { fmt x = fm /\ is_not_nan x /\ le_dz (fp_div m x y) b }
5760
ensures { le_dz x h }
5761
= assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (ext x) (Fin v)) };
5762
assert { ele (ediv (ext x) (Fin v)) (ext t) };
5763
ediv_move_le (ext t) (ext x) v;
5764
let ghost zx = if is_positive x then false else true in
5765
convert_widen fm' rtn x;
5766
assert { fp_convert fm' rtn x = round_f fm' rtn zx (ext x) };
5767
round_f_ele fm' rtn zx (mul_zneg t y') (ext x) (emul (ext t) (Fin v));
5768
assert { ele (ext x) (ext q) };
5769
let ghost zq = if is_positive q then false else true in
5770
round_f_ele fm rtn zx zq (ext x) (ext q);
5771
assert { round_f fm rtn zx (ext x) = x };
5772
assert { ele (ext x) (ext h) };
5773
ele_le x h in
5774
{qed}
5775
div_trim_hi fm minf m b y h 2
5776
end

The x of sign s with x / y in [a, b] for some y of [c, d], finite, not zero and of sign q

  Valid (1.4s) (colibri2 70) (cvc4 2) (cvc5 24) (split_vc 2) (depth 2)
5780
let
div_back_num_fin
(ghost fm: format) (m: mode) (s: bool) (lo hi lo' hi': t)
5781
(q: bool) (a b c d: t) : dom
5782
requires { region fm s lo hi /\ region fm (not s) lo' hi' }
5783
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
5784
requires { le_dz a b /\ le_dz c d /\ fin_nz c /\ fin_nz d }
5785
requires { (is_positive c <-> q) /\ (is_positive d <-> q) }
5786
ensures { result.fm = fm }
5787
ensures { forall x y. in_iv x lo hi -> in_iv y c d ->
5788
in_iv (fp_div m x y) a b -> mem x result }
5789
= {proof…}
5790
le_not_nan a b; le_not_nan c d;
5791
region_facts fm s lo hi lo; region_facts fm (not s) lo' hi' lo;
5792
let lemma yfacts (y: t) : unit
5793
requires { in_iv y c d }
5794
ensures { fin_nz y /\ fmt y = fm /\ (is_positive y <-> is_positive c) }
5795
= le_not_nan c y; index_range c; index_range d; index_range y in
5796
{qed}
5797
let ya = if s then c else d in
5798
let yb = if s then d else c in
5799
{proof…}
5800
let lemma ext (x y: t) : unit
5801
requires { in_iv x lo hi /\ in_iv y c d }
5802
ensures { le_dz (fp_div m x y) (fp_div m x ya) }
5803
ensures { le_dz (fp_div m x yb) (fp_div m x y) }
5804
= yfacts y; region_facts fm s lo hi x;
5805
le_refl x; div_special m x y; div_special m x c; div_special m x d;
5806
if is_positive c then begin
5807
if s then begin
5808
div_mag m true true x x c y; div_mag m true true x x y d
5809
end else begin
5810
div_mag m false true x x c y; div_mag m false true x x y d
5811
end
5812
end else begin
5813
assert { mag_le false y c /\ mag_le false d y };
5814
if s then begin
5815
div_mag m true false x x y c; div_mag m true false x x d y
5816
end else begin
5817
div_mag m false false x x y c; div_mag m false false x x d y
5818
end
5819
end in
5820
{qed}
5821
{proof…}
5822
region_facts fm s lo hi lo; region_facts fm s lo hi hi;
5823
region_facts fm (not s) lo' hi' lo';
5824
index_range lo; index_range hi; index_range lo'; index_range hi';
5825
{qed}
5826
let fr = format_of a in
5827
let minf = if s then lo' else lo in
5828
let pinf = if s then hi else hi' in
5829
let nz = if s then hi' else hi in
5830
let pz = if s then lo else lo' in
5831
{proof…}
5832
assert { minf = minus_infinity fm /\ pinf = plus_infinity fm };
5833
assert { at_index fm nz (-1) /\ at_index fm pz 0 };
5834
{qed}
5835
match widen fr with
5836
| None -> interval fm lo hi
5837
| Some fm' ->
5838
let (l0, h0) =
5839
if q then
5840
(div_lo_pos fm fm' minf pinf nz pz m a ya, div_hi_pos fm fm' minf pinf nz pz m b yb)
5841
else begin
5842
{proof…}
5843
yfacts c; yfacts d; le_refl c; le_refl d;
5844
assert { fin_nz (neg ya) /\ is_positive (neg ya) /\ fmt (neg ya) = fm };
5845
assert { fin_nz (neg yb) /\ is_positive (neg yb) /\ fmt (neg yb) = fm };
5846
{qed}
5847
(neg (div_hi_pos fm fm' minf pinf nz pz m b (neg yb)),
5848
neg (div_lo_pos fm fm' minf pinf nz pz m a (neg ya)))
5849
end in
5850
let l = max_diff_zero l0 lo in
5851
let h = min_diff_zero h0 hi in
5852
{proof…}
5853
let lemma pt (x y: t) : unit
5854
requires { in_iv x lo hi /\ in_iv y c d /\ in_iv (fp_div m x y) a b }
5855
ensures { le_dz l x /\ le_dz x h }
5856
= ext x y; le_trans a (fp_div m x y) (fp_div m x ya);
5857
le_trans (fp_div m x yb) (fp_div m x y) b;
5858
region_facts fm s lo hi x;
5859
if not q then begin
5860
yfacts c; yfacts d; le_refl c; le_refl d;
5861
div_neg_neg m x ya; div_neg_neg m x yb;
5862
index_range x; index_range (neg x); neg_neg x;
5863
assert { le_dz (neg h0) (neg x) /\ le_dz (neg x) (neg l0) };
5864
neg_le (neg h0) (neg x); neg_le (neg x) (neg l0);
5865
neg_neg h0; neg_neg l0
5866
end in
5867
{qed}
5868
if lt_diff_zero h l then begin
5869
{proof…}
5870
let lemma no (x y: t) : unit
5871
requires { in_iv x lo hi /\ in_iv y c d /\ in_iv (fp_div m x y) a b }
5872
ensures { false }
5873
= pt x y; le_trans l x h; lt_not_le h l in
5874
{qed}
5875
empty_dom fm
5876
end else begin
5877
{proof…} total l h; {qed}
5878
interval fm l h
5879
end
5880
end

The x with x / y in [a, b] for some y of [c, d]

  Valid (2.4s) (colibri2 78) (cvc4 17) (cvc5 84) (split_vc 89) (depth 5)
5883
let
div_back_num_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b c d: t) : dom
5884
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5885
requires { specials4 fm minf pinf nz pz }
5886
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
5887
requires { le_dz a b /\ le_dz c d }
5888
ensures { result.fm = fm }
5889
ensures { forall x y. fmt x = fm -> is_not_nan x -> in_iv y c d ->
5890
in_iv (fp_div m x y) a b -> mem x result }
5891
= {proof…}
5892
le_not_nan a b; le_not_nan c d; format_facts fm ();
5893
index_range minf; index_range pinf; index_range nz; index_range pz;
5894
of_index_range fm 1; of_index_range fm (-2);
5895
of_index_range fm (nb_sign fm - 2); of_index_range fm (1 - nb_sign fm);
5896
succ_gt pz; pred_lt pinf; succ_gt minf; pred_lt nz;
5897
{qed}
5898
let psub = succ_diff_zero pz in
5899
let pmax = pred_diff_zero pinf in
5900
let nmin = succ_diff_zero minf in
5901
let nmax = pred_diff_zero nz in
5902
{proof…}
5903
assert { index psub = 1 /\ index pmax = nb_sign fm - 2 };
5904
assert { index nmin = 1 - nb_sign fm /\ index nmax = -2 };
5905
region_facts fm true pz pinf pz; region_facts fm false minf nz pz;
5906
{qed}
5907
let c1 = max_diff_zero c psub in
5908
let d1 = min_diff_zero d pmax in
5909
let fp =
5910
if le_diff_zero c1 d1 then begin
5911
{proof…} index_range c1; index_range d1; {qed}
5912
union fm (div_back_num_fin fm m true pz pinf minf nz true a b c1 d1)
5913
(div_back_num_fin fm m false minf nz pz pinf true a b c1 d1)
5914
end else empty_dom fm in
5915
let c2 = max_diff_zero c nmin in
5916
let d2 = min_diff_zero d nmax in
5917
let fn =
5918
if le_diff_zero c2 d2 then begin
5919
{proof…} index_range c2; index_range d2; {qed}
5920
union fm (div_back_num_fin fm m true pz pinf minf nz false a b c2 d2)
5921
(div_back_num_fin fm m false minf nz pz pinf false a b c2 d2)
5922
end else empty_dom fm in
5923
(* the zeros: a nonzero x gives an infinity *)
5924
let im = in_ivf minf a b in
5925
let ip = in_ivf pinf a b in
5926
let z1 = if in_ivf nz c d then union fm (iv_if fm im psub pinf) (iv_if fm ip minf nmax)
5927
else empty_dom fm in
5928
let z2 = if in_ivf pz c d then union fm (iv_if fm ip psub pinf) (iv_if fm im minf nmax)
5929
else empty_dom fm in
5930
(* the infinities: a finite x gives a zero *)
5931
let zn = in_ivf nz a b in
5932
let zp = in_ivf pz a b in
5933
let i1 = if in_ivf minf c d then union fm (iv_if fm zn pz pmax) (iv_if fm zp nmin nz)
5934
else empty_dom fm in
5935
let i2 = if in_ivf pinf c d then union fm (iv_if fm zp pz pmax) (iv_if fm zn nmin nz)
5936
else empty_dom fm in
5937
let res = union fm (union fm fp fn) (union fm (union fm z1 z2) (union fm i1 i2)) in
5938
{proof…}
5939
let lemma pt (x y: t) : unit
5940
requires { fmt x = fm /\ is_not_nan x /\ in_iv y c d /\ in_iv (fp_div m x y) a b }
5941
ensures { mem x res }
5942
= le_not_nan c y; le_not_nan a (fp_div m x y);
5943
index_range x; index_range y;
5944
region_facts fm true pz pinf x; region_facts fm false minf nz x;
5945
div_special m x y;
5946
if pure { is_zero y } then begin
5947
if pure { is_positive y } then
5948
assert { y = pz by of_index fm (index y) = of_index fm (index pz) }
5949
else
5950
assert { y = nz by of_index fm (index y) = of_index fm (index nz) };
5951
assert { not is_zero x };
5952
assert { is_positive x -> le_dz psub x };
5953
assert { not is_positive x -> le_dz x nmax };
5954
assert { fp_div m x y = minf \/ fp_div m x y = pinf };
5955
assert { mem x z1 \/ mem x z2 }
5956
end else if pure { is_infinity y } then begin
5957
if pure { is_plus_infinity y } then plus_infinity_unique y
5958
else minus_infinity_unique y;
5959
if pure { is_zero (fp_div m x y) } then begin
5960
let r = fp_div m x y in
5961
index_range r;
5962
if pure { is_positive r } then
5963
assert { r = pz by of_index fm (index r) = of_index fm (index pz) }
5964
else
5965
assert { r = nz by of_index fm (index r) = of_index fm (index nz) }
5966
end;
5967
assert { mem x i1 \/ mem x i2 }
5968
end else if pure { is_positive y } then begin
5969
assert { in_iv y c1 d1 };
5970
assert { mem x fp }
5971
end else begin
5972
assert { in_iv y c2 d2 };
5973
assert { mem x fn }
5974
end in
5975
{qed}
5976
res
5977
Valid (455ms) (cvc5)
5978
let rec
div_back_num_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b: t)
5979
(l: list (t, t)) : dom
5980
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5981
requires { specials4 fm minf pinf nz pz }
5982
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l /\ fmt_u fm l }
5983
ensures { result.fm = fm }
5984
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l ->
5985
in_iv (fp_div m x y) a b -> mem x result }
5986
variant { l }
5987
= match l with
5988
| Nil -> empty_dom fm
5989
| Cons (c, d) r ->
5990
union fm (div_back_num_iv fm minf pinf nz pz m a b c d)
5991
(div_back_num_iv_u fm minf pinf nz pz m a b r)
5992
end
5993
Valid (504ms) (cvc5)
5994
let rec
div_back_num_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
5995
(l1 l2: list (t, t)) : dom
5996
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
5997
requires { specials4 fm minf pinf nz pz }
5998
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
5999
ensures { result.fm = fm }
6000
ensures { forall x y. fmt x = fm -> is_not_nan x -> mem_u y l2 ->
6001
mem_u (fp_div m x y) l1 -> mem x result }
6002
variant { l1 }
6003
= match l1 with
6004
| Nil -> empty_dom fm
6005
| Cons (a, b) r ->
6006
union fm (div_back_num_iv_u fm minf pinf nz pz m a b l2)
6007
(div_back_num_u fm minf pinf nz pz m r l2)
6008
end

The x such that x / y is in r for some y in d

  Valid (573ms) (colibri2 15) (cvc4 4) (cvc5 17) (split_vc) (depth 1)
6011
let
div_back_num
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r d: dom) : dom
6012
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6013
requires { specials4 fm minf pinf nz pz }
6014
requires { r.fm = fm /\ d.fm = fm }
6015
ensures { result.fm = fm }
6016
ensures { forall x y. fmt x = fm -> mem y d -> mem (fp_div m x y) r -> mem x result }
6017
= {proof…}
6018
index_range minf; index_range pinf; index_range nz; index_range pz;
6019
{qed}
6020
let u = div_back_num_u fm minf pinf nz pz m r.u d.u in
6021
let n =
6022
if r.nan then
6023
if d.nan then top fm minf pinf
6024
else begin
6025
let p = if meets fm nz pz d
6026
then union fm (singleton fm nz) (singleton fm pz) else empty_dom fm in
6027
let q = if meets fm minf minf d || meets fm pinf pinf d
6028
then union fm (singleton fm minf) (singleton fm pinf) else empty_dom fm in
6029
union fm (nan_dom fm) (union fm p q)
6030
end
6031
else empty_dom fm in
6032
let res = union fm n u in
6033
{proof…}
6034
let lemma pt (x y: t) : unit
6035
requires { fmt x = fm /\ mem y d /\ mem (fp_div m x y) r }
6036
ensures { mem x res }
6037
= div_special m x y; mul_div_fmt m x y;
6038
if pure { is_nan (fp_div m x y) } then begin
6039
if pure { is_not_nan x /\ is_not_nan y } then begin
6040
index_range x; index_range y;
6041
if pure { is_zero x } then begin
6042
if pure { is_positive x } then
6043
assert { x = pz by of_index fm (index x) = of_index fm (index pz) }
6044
else
6045
assert { x = nz by of_index fm (index x) = of_index fm (index nz) };
6046
assert { index nz <= index y <= index pz }
6047
end else begin
6048
if pure { is_plus_infinity x } then plus_infinity_unique x
6049
else minus_infinity_unique x;
6050
if pure { is_plus_infinity y } then plus_infinity_unique y
6051
else minus_infinity_unique y
6052
end
6053
end
6054
end in
6055
{qed}
6056
res
Backward division: the divisor

The y such that x / y is in [a, b] for some x in [c, d]. On the finite nonzero x of one sign q, x / y is monotonic in y on the floats of one sign s: increasing for a negative x, decreasing for a positive one.

Direct bounds of the backward division: the divisor

For x finite and not zero and y positive, x / y is decreasing in y for a positive x, increasing for a negative one. The y with x / y <= b (positive x) or a <= x / y (negative x) are at or above x / t, t the threshold of b (resp. a) in fm', when t has the sign of x, rounded up in fm' then in fm; at or below for the other end. +0 gives an infinite quotient, +inf a zero one. Then at most two steps; None when no y is left. A zero end of the wrong sign leaves no y: the quotient has the sign of x. A negative y is the positive -y on -x. The same results as the former bisection (Search) on 300000 random cases of 3 formats and the 5 modes (2026–11–09), for both the dividend and the divisor.

The divisor from a bound of the quotient, u positive

  Valid (144ms) (colibri2)
6081
let ghost
ediv_den_pos
(u w: real) (e: ereal) : unit
6082
requires { 0.0 <. u /\ 0.0 <. w }
6083
ensures { ele (ediv (Fin u) (Fin w)) e ->
6084
ele (Fin 0.0) e /\ e <> Fin 0.0 /\ ele (ediv (Fin u) e) (Fin w) }
6085
ensures { ele e (ediv (Fin u) (Fin w)) -> ele (Fin 0.0) e -> e <> Fin 0.0 ->
6086
ele (Fin w) (ediv (Fin u) e) }
6087
= {proof…}
6088
assert { ediv (Fin u) (Fin w) = Fin (u *. (1.0 /. w)) /\ 0.0 <. 1.0 /. w };
6089
assert { 0.0 <. u *. (1.0 /. w) };
6090
assert { u *. (1.0 /. w) *. w = u };
6091
match e with
6092
| Fin r ->
6093
if pure { 0.0 <. r } then begin
6094
assert { ediv (Fin u) e = Fin (u *. (1.0 /. r)) };
6095
assert { u *. (1.0 /. r) *. r = u };
6096
assert { u *. (1.0 /. w) <=. r -> u <=. r *. w };
6097
assert { r <=. u *. (1.0 /. w) -> r *. w <=. u };
6098
assert { u <=. r *. w -> u *. (1.0 /. r) <=. w };
6099
assert { r *. w <=. u -> w <=. u *. (1.0 /. r) }
6100
end
6101
| PInf -> assert { ediv (Fin u) e = Fin 0.0 }
6102
| MInf -> ()
6103
end
6104
{qed}

The divisor from a bound of the quotient, u negative

  Valid (65ms) (colibri2 15) (split_vc) (depth 1)
6107
let ghost
ediv_den_neg
(u w: real) (e: ereal) : unit
6108
requires { u <. 0.0 /\ 0.0 <. w }
6109
ensures { ele e (ediv (Fin u) (Fin w)) ->
6110
ele e (Fin 0.0) /\ e <> Fin 0.0 /\ ele (ediv (Fin u) e) (Fin w) }
6111
ensures { ele (ediv (Fin u) (Fin w)) e -> ele e (Fin 0.0) -> e <> Fin 0.0 ->
6112
ele (Fin w) (ediv (Fin u) e) }
6113
= {proof…}
6114
assert { ediv (Fin u) (Fin w) = Fin (u *. (1.0 /. w)) /\ 0.0 <. 1.0 /. w };
6115
assert { u *. (1.0 /. w) <. 0.0 };
6116
assert { u *. (1.0 /. w) *. w = u };
6117
match e with
6118
| Fin r ->
6119
if pure { r <. 0.0 } then begin
6120
assert { ediv (Fin u) e = Fin (u *. (1.0 /. r)) };
6121
assert { u *. (1.0 /. r) *. r = u };
6122
assert { r <=. u *. (1.0 /. w) -> r *. w <=. u };
6123
assert { u *. (1.0 /. w) <=. r -> u <=. r *. w };
6124
assert { r *. w <=. u -> u *. (1.0 /. r) <=. w };
6125
assert { 1.0 /. r <. 0.0 /\ r *. (1.0 /. r) = 1.0 };
6126
assert { (r *. w) *. (1.0 /. r) = w *. (r *. (1.0 /. r)) };
6127
assert { u <=. r *. w -> (r *. w) *. (1.0 /. r) <=. u *. (1.0 /. r) };
6128
assert { u <=. r *. w -> w <=. u *. (1.0 /. r) }
6129
end
6130
| MInf -> assert { ediv (Fin u) e = Fin 0.0 }
6131
| PInf -> ()
6132
end
6133
{qed}

The constraint on a divisor: x / y <= b for a positive x, a <= x / y for a negative one

6137
predicate
den_lo_c
(m: mode) (a b x y: t) =
6138
if is_positive x then le_dz (fp_div m x y) b else le_dz a (fp_div m x y)

The other end: a <= x / y for a positive x, x / y <= b for a negative one

6142
predicate
den_hi_c
(m: mode) (a b x y: t) =
6143
if is_positive x then le_dz a (fp_div m x y) else le_dz (fp_div m x y) b

At most fuel steps up from l while l fails the constraint; None when +inf fails it too

  Valid (995ms) (cvc5)
6147
let rec
den_trim_lo
(ghost fm: format) (pinf: t) (m: mode) (px: bool) (a b x l: t) (fuel: int)
6148
: option t
6149
requires { fmt pinf = fm /\ pinf = plus_infinity fm }
6150
requires { fmt a = fm /\ fmt b = fm /\ fmt x = fm /\ fmt l = fm /\ is_not_nan l /\ fin_nz x }
6151
requires { px <-> is_positive x }
6152
requires { forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_lo_c m a b x y -> le_dz l y }
6153
ensures { match result with
6154
| Some r -> fmt r = fm /\ is_not_nan r /\
6155
forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_lo_c m a b x y -> le_dz r y
6156
| None -> forall y. fmt y = fm -> is_not_nan y -> is_positive y -> not den_lo_c m a b x y
6157
end }
6158
variant { fuel }
6159
= if fuel <= 0 then Some l
6160
else if (if px then lt_diff_zero b (div m x l) else lt_diff_zero (div m x l) a)
6161
then begin
6162
{proof…}
6163
let lemma pt (y: t) : unit
6164
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6165
ensures { lt_dz l y }
6166
= if is_positive x then lt_not_le b (fp_div m x l) else lt_not_le (fp_div m x l) a;
6167
if pure { y = l } then absurd;
6168
index_range y; index_range l in
6169
{qed}
6170
if is_pinf fm pinf l then begin
6171
{proof…}
6172
let lemma no (y: t) : unit
6173
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6174
ensures { false }
6175
= pt y; index_range y; index_range pinf in
6176
{qed}
6177
None
6178
end else begin
6179
{proof…}
6180
let lemma pt2 (y: t) : unit
6181
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6182
ensures { le_dz (succ_dz l) y }
6183
= pt y; index_range y; index_range l; succ_least l y in
6184
index_range l; index_range pinf; of_index_range fm (index l + 1);
6185
{qed}
6186
den_trim_lo fm pinf m px a b x (succ_diff_zero l) (fuel - 1)
6187
end
6188
end else Some l

At most fuel steps down from h while h fails the constraint; None when +0 fails it too

  Valid (1.2s) (cvc4)
6192
let rec
den_trim_hi
(ghost fm: format) (pz: t) (m: mode) (px: bool) (a b x h: t) (fuel: int)
6193
: option t
6194
requires { fmt pz = fm /\ at_index fm pz 0 }
6195
requires { fmt a = fm /\ fmt b = fm /\ fmt x = fm /\ fmt h = fm /\ is_not_nan h /\ fin_nz x }
6196
requires { px <-> is_positive x }
6197
requires { forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_hi_c m a b x y -> le_dz y h }
6198
ensures { match result with
6199
| Some r -> fmt r = fm /\ is_not_nan r /\
6200
forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_hi_c m a b x y -> le_dz y r
6201
| None -> forall y. fmt y = fm -> is_not_nan y -> is_positive y -> not den_hi_c m a b x y
6202
end }
6203
variant { fuel }
6204
= if fuel <= 0 then Some h
6205
else if (if px then lt_diff_zero (div m x h) a else lt_diff_zero b (div m x h))
6206
then begin
6207
{proof…}
6208
let lemma pt (y: t) : unit
6209
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_hi_c m a b x y }
6210
ensures { lt_dz y h }
6211
= if is_positive x then lt_not_le (fp_div m x h) a else lt_not_le b (fp_div m x h);
6212
if pure { y = h } then absurd;
6213
index_range y; index_range h in
6214
{qed}
6215
if le_diff_zero h pz then begin
6216
{proof…}
6217
let lemma no (y: t) : unit
6218
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_hi_c m a b x y }
6219
ensures { false }
6220
= pt y; index_range y; index_range h in
6221
{qed}
6222
None
6223
end else begin
6224
{proof…}
6225
let lemma pt2 (y: t) : unit
6226
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_hi_c m a b x y }
6227
ensures { le_dz y (pred_dz h) }
6228
= pt y; index_range y; index_range h; pred_greatest h y in
6229
index_range h; of_index_range fm (index h - 1);
6230
{qed}
6231
den_trim_hi fm pz m px a b x (pred_diff_zero h) (fuel - 1)
6232
end
6233
end else Some h

A lower bound of the positive y with den_lo_c m a b x y

  Valid (1.9s) (colibri2 68) (cvc4 22) (cvc5 37) (split_vc 4) (depth 2)
6236
let
den_lo
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (a b x: t) : option t
6237
requires { widens fm fm' /\ fmt a = fm /\ fmt b = fm /\ fmt x = fm }
6238
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6239
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
6240
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
6241
requires { is_not_nan a /\ is_not_nan b /\ fin_nz x }
6242
ensures { match result with
6243
| Some r -> fmt r = fm /\ is_not_nan r /\
6244
forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_lo_c m a b x y -> le_dz r y
6245
| None -> forall y. fmt y = fm -> is_not_nan y -> is_positive y -> not den_lo_c m a b x y
6246
end }
6247
= {proof…} index_range x; index_range pz; index_range pinf; {qed}
6248
let px = le_diff_zero pz x in
6249
if (if px then is_pinf fm pinf b else is_minf fm minf a) then begin
6250
{proof…}
6251
let lemma pt (y: t) : unit
6252
requires { fmt y = fm /\ is_not_nan y /\ is_positive y } ensures { le_dz pz y }
6253
= index_range y in
6254
{qed}
6255
Some pz
6256
end else if (if px then lt_diff_zero b pz else lt_diff_zero nz a) then begin
6258
(* the quotient has the sign of `x`: never below +0 for a positive `x`, never above -0 for a negative one *)
6259
{proof…}
6260
let lemma no (y: t) : unit
6261
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6262
ensures { false }
6263
= index_range y; index_range a; index_range b; index_range nz; index_range pz;
6264
div_special m x y; mul_div_fmt m x y;
6265
let ghost u = fin_val x in
6266
assert { px -> 0.0 <. u by ele (ext pz) (ext x) /\ ext x <> Fin 0.0 };
6267
assert { not px -> u <. 0.0 by ele (ext x) (ext nz) /\ ext x <> Fin 0.0 };
6268
let ghost r = fp_div m x y in
6269
let ghost zn = if px then false else true in
6270
assert { mul_zneg x y = zn };
6271
let ghost z0 = round_f fm m zn (Fin 0.0) in
6272
assert { is_zero z0 /\ fmt z0 = fm /\ is_not_nan z0 /\ (is_positive z0 <-> px) };
6273
index_range z0; index_range r;
6274
if pure { is_zero y } then begin
6275
assert { r = if px then plus_infinity fm else minus_infinity fm };
6276
assert { px -> le_dz pz r };
6277
assert { not px -> le_dz r nz }
6278
end else begin
6279
let ghost e = ediv (Fin u) (ext y) in
6280
assert { r = round_f fm m zn e };
6281
if pure { is_infinity y } then begin
6282
plus_infinity_unique y; assert { e = Fin 0.0 }
6283
end else begin
6284
let ghost w = fin_val y in
6285
assert { 0.0 <. w by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
6286
assert { 0.0 <. 1.0 /. w };
6287
assert { e = Fin (u *. (1.0 /. w)) }
6288
end;
6289
if px then begin
6290
assert { ele (Fin 0.0) e }; assert { le_dz z0 r };
6291
assert { z0 = pz by index z0 = 0 /\ of_index fm (index z0) = of_index fm (index pz) }
6292
end else begin
6293
assert { ele e (Fin 0.0) }; assert { le_dz r z0 };
6294
assert { z0 = nz by index z0 = -1 /\ of_index fm (index z0) = of_index fm (index nz) }
6295
end
6296
end;
6297
if px then begin le_trans pz r b; lt_not_le b pz end
6298
else begin le_trans a r nz; lt_not_le nz a end in
6299
{qed}
6300
None
6301
end else begin
6302
let fr = format_of x in
6303
let t = if px then thr_hi fm fm' nz pz m b else thr_lo fm fm' nz pz m a in
6304
{proof…}
6305
format_facts fm' (); of_index_range fm' 0; of_index_range fm' (-1);
6306
{qed}
6307
let pz' = of_index_v fm' 0 in
6308
let nz' = of_index_v fm' (-1) in
6309
if (if px then lt_diff_zero pz' t else lt_diff_zero t nz') then begin
6310
let x' = convert fm' rtn x in
6311
{proof…}
6312
convert_widen fm' rtn x;
6313
let ghost u = fin_val x in
6314
assert { px -> 0.0 <. u by ele (ext pz) (ext x) /\ ext x <> Fin 0.0 };
6315
assert { not px -> u <. 0.0 by ele (ext x) (ext nz) /\ ext x <> Fin 0.0 };
6316
assert { ext x' = Fin u /\ fmt x' = fm' /\ fin_nz x' };
6317
index_range t; index_range pz'; index_range nz';
6318
assert { not is_zero t };
6319
{qed}
6320
let q = div rtp x' t in
6321
let l0 = convert fr rtp q in
6322
{proof…} convert_not_nan fm rtp q; {qed}
6323
let l = max_diff_zero l0 pz in
6324
{proof…}
6325
let lemma pt (y: t) : unit
6326
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6327
ensures { le_dz l y }
6328
= index_range y; div_special m x y;
6329
if pure { is_zero y } then begin
6330
assert { y = pz by of_index fm (index y) = of_index fm (index pz) };
6331
if px then plus_infinity_unique b else minus_infinity_unique a;
6332
absurd
6333
end else if pure { is_infinity y } then begin
6334
plus_infinity_unique y; index_range l
6335
end else begin
6336
let ghost w = fin_val y in
6337
assert { 0.0 <. w by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
6338
assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (Fin u) (Fin w)) };
6339
if px then begin
6340
assert { ele (ediv (Fin u) (Fin w)) (ext t) };
6341
ediv_den_pos u w (ext t)
6342
end else begin
6343
assert { ele (ext t) (ediv (Fin u) (Fin w)) };
6344
ediv_den_neg u w (ext t)
6345
end;
6346
assert { ele (ediv (Fin u) (ext t)) (Fin w) };
6347
assert { ext q = ext (round_f fm' rtp (mul_zneg x' t) (ediv (Fin u) (ext t))) };
6348
let ghost zy = if is_positive y then false else true in
6349
convert_widen fm' rtp y;
6350
assert { fp_convert fm' rtp y = round_f fm' rtp zy (ext y) };
6351
round_f_ele fm' rtp (mul_zneg x' t) zy (ediv (Fin u) (ext t)) (ext y);
6352
assert { ele (ext q) (ext y) };
6353
let ghost zq = if is_positive q then false else true in
6354
round_f_ele fm rtp zq zy (ext q) (ext y);
6355
assert { round_f fm rtp zy (ext y) = y };
6356
assert { ele (ext l0) (ext y) };
6357
ele_le l0 y; le_trans pz pz y
6358
end in
6359
{qed}
6360
den_trim_lo fm pinf m px a b x l 2
6361
end else begin
6362
(* no finite `y`: the quotient would have the sign of `x` *)
6363
{proof…}
6364
let lemma pt (y: t) : unit
6365
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_lo_c m a b x y }
6366
ensures { le_dz pinf y }
6367
= index_range y; div_special m x y; index_range t; index_range pz'; index_range nz';
6368
let ghost u = fin_val x in
6369
assert { px -> 0.0 <. u by ele (ext pz) (ext x) /\ ext x <> Fin 0.0 };
6370
assert { not px -> u <. 0.0 by ele (ext x) (ext nz) /\ ext x <> Fin 0.0 };
6371
if pure { is_zero y } then begin
6372
assert { y = pz by of_index fm (index y) = of_index fm (index pz) };
6373
if px then plus_infinity_unique b else minus_infinity_unique a;
6374
absurd
6375
end else if pure { is_infinity y } then begin
6376
plus_infinity_unique y
6377
end else begin
6378
let ghost w = fin_val y in
6379
assert { 0.0 <. w by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
6380
assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (Fin u) (Fin w)) };
6381
if px then begin
6382
assert { ele (ext t) (ext pz') };
6383
assert { ele (ediv (Fin u) (Fin w)) (ext t) };
6384
ediv_den_pos u w (ext t);
6385
assert { ext t <> Fin 0.0 -> not is_zero t };
6386
absurd
6387
end else begin
6388
assert { ele (ext nz') (ext t) };
6389
assert { ele (ext t) (ediv (Fin u) (Fin w)) };
6390
ediv_den_neg u w (ext t);
6391
absurd
6392
end
6393
end in
6394
{qed}
6395
den_trim_lo fm pinf m px a b x pinf 1
6396
end
6397
end

An upper bound of the positive y with den_hi_c m a b x y

  Valid (1.5s) (colibri2 44) (cvc4 8) (cvc5 18) (split_vc 2) (depth 2)
6400
let
den_hi
(ghost fm: format) (fm': format) (minf pinf nz pz: t) (m: mode) (a b x: t) : option t
6401
requires { widens fm fm' /\ fmt a = fm /\ fmt b = fm /\ fmt x = fm }
6402
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6403
requires { minf = minus_infinity fm /\ pinf = plus_infinity fm }
6404
requires { at_index fm nz (-1) /\ at_index fm pz 0 }
6405
requires { is_not_nan a /\ is_not_nan b /\ fin_nz x }
6406
ensures { match result with
6407
| Some r -> fmt r = fm /\ is_not_nan r /\
6408
forall y. fmt y = fm -> is_not_nan y -> is_positive y -> den_hi_c m a b x y -> le_dz y r
6409
| None -> forall y. fmt y = fm -> is_not_nan y -> is_positive y -> not den_hi_c m a b x y
6410
end }
6411
= {proof…}
6412
index_range x; index_range pz; index_range pinf;
6413
let lemma top (y: t) : unit
6414
requires { fmt y = fm /\ is_not_nan y } ensures { le_dz y pinf }
6415
= index_range y in
6416
{qed}
6417
let px = le_diff_zero pz x in
6418
if (if px then is_minf fm minf a else is_pinf fm pinf b) then Some pinf
6419
else begin
6420
let fr = format_of x in
6421
let t = if px then thr_lo fm fm' nz pz m a else thr_hi fm fm' nz pz m b in
6422
{proof…}
6423
format_facts fm' (); of_index_range fm' 0; of_index_range fm' (-1);
6424
{qed}
6425
let pz' = of_index_v fm' 0 in
6426
let nz' = of_index_v fm' (-1) in
6427
if (if px then lt_diff_zero pz' t else lt_diff_zero t nz') then begin
6428
let x' = convert fm' rtn x in
6429
{proof…}
6430
convert_widen fm' rtn x;
6431
let ghost u = fin_val x in
6432
assert { px -> 0.0 <. u by ele (ext pz) (ext x) /\ ext x <> Fin 0.0 };
6433
assert { not px -> u <. 0.0 by ele (ext x) (ext nz) /\ ext x <> Fin 0.0 };
6434
assert { ext x' = Fin u /\ fmt x' = fm' /\ fin_nz x' };
6435
index_range t; index_range pz'; index_range nz';
6436
assert { not is_zero t };
6437
{qed}
6438
let q = div rtn x' t in
6439
let h0 = convert fr rtn q in
6440
{proof…} convert_not_nan fm rtn q; {qed}
6441
let h = max_diff_zero h0 pz in
6442
{proof…}
6443
let lemma pt (y: t) : unit
6444
requires { fmt y = fm /\ is_not_nan y /\ is_positive y /\ den_hi_c m a b x y }
6445
ensures { le_dz y h }
6446
= index_range y; div_special m x y;
6447
if pure { is_zero y } then begin
6448
assert { y = pz by of_index fm (index y) = of_index fm (index pz) };
6449
le_refl pz
6450
end else if pure { is_infinity y } then begin
6451
plus_infinity_unique y;
6452
assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (Fin u) PInf) };
6453
assert { ediv (Fin u) PInf = Fin 0.0 };
6454
if px then assert { ele (ext t) (Fin 0.0) }
6455
else assert { ele (Fin 0.0) (ext t) };
6456
assert { px -> ele (ext pz') (ext t) /\ ext t <> Fin 0.0 };
6457
assert { not px -> ele (ext t) (ext nz') /\ ext t <> Fin 0.0 };
6458
absurd
6459
end else begin
6460
let ghost w = fin_val y in
6461
assert { 0.0 <. w by ele (ext pz) (ext y) /\ ext y <> Fin 0.0 };
6462
assert { fp_div m x y = round_f fm m (mul_zneg x y) (ediv (Fin u) (Fin w)) };
6463
if px then begin
6464
assert { ele (ext t) (ediv (Fin u) (Fin w)) };
6465
assert { ele (ext pz') (ext t) /\ ext t <> Fin 0.0 };
6466
ediv_den_pos u w (ext t)
6467
end else begin
6468
assert { ele (ediv (Fin u) (Fin w)) (ext t) };
6469
assert { ele (ext t) (ext nz') /\ ext t <> Fin 0.0 };
6470
ediv_den_neg u w (ext t)
6471
end;
6472
assert { ele (Fin w) (ediv (Fin u) (ext t)) };
6473
assert { ext q = ext (round_f fm' rtn (mul_zneg x' t) (ediv (Fin u) (ext t))) };
6474
let ghost zy = if is_positive y then false else true in
6475
convert_widen fm' rtn y;
6476
assert { fp_convert fm' rtn y = round_f fm' rtn zy (ext y) };
6477
round_f_ele fm' rtn zy (mul_zneg x' t) (ext y) (ediv (Fin u) (ext t));
6478
assert { ele (ext y) (ext q) };
6479
let ghost zq = if is_positive q then false else true in
6480
round_f_ele fm rtn zy zq (ext y) (ext q);
6481
assert { round_f fm rtn zy (ext y) = y };
6482
assert { ele (ext y) (ext h0) };
6483
ele_le y h0
6484
end in
6485
{qed}
6486
den_trim_hi fm pz m px a b x h 2
6487
end else den_trim_hi fm pz m px a b x pinf 2
6488
end

The y of sign s with x / y in [a, b] for some x of [c, d], finite, not zero and of sign q

  Valid (946ms) (colibri2 75) (cvc4 6) (cvc5 19) (split_vc) (depth 1)
6492
let
div_back_den_fin
(ghost fm: format) (m: mode) (s: bool) (lo hi lo' hi': t)
6493
(q: bool) (a b c d: t) : dom
6494
requires { region fm s lo hi /\ region fm (not s) lo' hi' }
6495
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
6496
requires { le_dz a b /\ le_dz c d /\ fin_nz c /\ fin_nz d }
6497
requires { (is_positive c <-> q) /\ (is_positive d <-> q) }
6498
ensures { result.fm = fm }
6499
ensures { forall x y. in_iv y lo hi -> in_iv x c d ->
6500
in_iv (fp_div m x y) a b -> mem y result }
6501
= {proof…}
6502
le_not_nan a b; le_not_nan c d;
6503
region_facts fm s lo hi lo; region_facts fm (not s) lo' hi' lo;
6504
let lemma xfacts (x: t) : unit
6505
requires { in_iv x c d }
6506
ensures { fin_nz x /\ fmt x = fm /\ (is_positive x <-> is_positive c) }
6507
= le_not_nan c x; index_range c; index_range d; index_range x in
6508
{qed}
6509
let xa = if s then d else c in
6510
let xb = if s then c else d in
6511
{proof…}
6512
let lemma ext (x y: t) : unit
6513
requires { in_iv y lo hi /\ in_iv x c d }
6514
ensures { le_dz (fp_div m x y) (fp_div m xa y) }
6515
ensures { le_dz (fp_div m xb y) (fp_div m x y) }
6516
= xfacts x; region_facts fm s lo hi y;
6517
le_refl y; div_special m x y; div_special m c y; div_special m d y;
6518
if is_positive c then begin
6519
if s then begin
6520
div_mag m true true x d y y; div_mag m true true c x y y
6521
end else begin
6522
div_mag m true false x d y y; div_mag m true false c x y y
6523
end
6524
end else begin
6525
assert { mag_le false x c /\ mag_le false d x };
6526
if s then begin
6527
div_mag m false true x c y y; div_mag m false true d x y y
6528
end else begin
6529
div_mag m false false x c y y; div_mag m false false d x y y
6530
end
6531
end in
6532
{qed}
6533
{proof…}
6534
region_facts fm s lo hi lo; region_facts fm s lo hi hi;
6535
region_facts fm (not s) lo' hi' lo';
6536
index_range lo; index_range hi; index_range lo'; index_range hi';
6537
{qed}
6538
let fr = format_of a in
6539
let minf = if s then lo' else lo in
6540
let pinf = if s then hi else hi' in
6541
let nz = if s then hi' else hi in
6542
let pz = if s then lo else lo' in
6543
{proof…}
6544
assert { minf = minus_infinity fm /\ pinf = plus_infinity fm };
6545
assert { at_index fm nz (-1) /\ at_index fm pz 0 };
6546
xfacts c; xfacts d; le_refl c; le_refl d;
6547
{qed}
6548
(* the `x` of the lower and the upper bound of a positive `y` *)
6549
let xl = if s then (if q then xb else xa) else (if q then xa else xb) in
6550
let xh = if s then (if q then xa else xb) else (if q then xb else xa) in
6551
match widen fr with
6552
| None -> interval fm lo hi
6553
| Some fm' ->
6554
let (o1, o2) =
6555
if s then
6556
(den_lo fm fm' minf pinf nz pz m a b xl, den_hi fm fm' minf pinf nz pz m a b xh)
6557
else begin
6558
{proof…}
6559
assert { fin_nz (neg xl) /\ fmt (neg xl) = fm };
6560
assert { fin_nz (neg xh) /\ fmt (neg xh) = fm };
6561
{qed}
6562
(den_hi fm fm' minf pinf nz pz m a b (neg xh),
6563
den_lo fm fm' minf pinf nz pz m a b (neg xl))
6564
end in
6565
{proof…}
6566
(* the constraints on a positive divisor *)
6567
let lemma cs (x y: t) : unit
6568
requires { in_iv y lo hi /\ in_iv x c d /\ in_iv (fp_div m x y) a b }
6569
ensures { s -> is_positive y /\ is_not_nan y /\ fmt y = fm /\
6570
den_lo_c m a b xl y /\ den_hi_c m a b xh y }
6571
ensures { not s -> is_positive (neg y) /\ is_not_nan (neg y) /\ fmt (neg y) = fm /\
6572
den_lo_c m a b (neg xl) (neg y) /\ den_hi_c m a b (neg xh) (neg y) }
6573
= ext x y; le_trans a (fp_div m x y) (fp_div m xa y);
6574
le_trans (fp_div m xb y) (fp_div m x y) b;
6575
region_facts fm s lo hi y; index_range y;
6576
if not s then begin
6577
index_range (neg y); neg_neg y;
6578
div_neg_neg m xa y; div_neg_neg m xb y
6579
end in
6580
{qed}
6581
match o1, o2 with
6582
| Some r1, Some r2 ->
6583
let l0 = if s then r1 else neg r1 in
6584
let h0 = if s then r2 else neg r2 in
6585
let l = max_diff_zero l0 lo in
6586
let h = min_diff_zero h0 hi in
6587
{proof…}
6588
let lemma pt (x y: t) : unit
6589
requires { in_iv y lo hi /\ in_iv x c d /\ in_iv (fp_div m x y) a b }
6590
ensures { le_dz l y /\ le_dz y h }
6591
= cs x y; region_facts fm s lo hi y;
6592
if not s then begin
6593
index_range y; neg_neg y;
6594
assert { le_dz (neg y) r1 /\ le_dz r2 (neg y) };
6595
neg_le (neg y) r1; neg_le r2 (neg y)
6596
end in
6597
{qed}
6598
if lt_diff_zero h l then begin
6599
{proof…}
6600
let lemma no (x y: t) : unit
6601
requires { in_iv y lo hi /\ in_iv x c d /\ in_iv (fp_div m x y) a b }
6602
ensures { false }
6603
= pt x y; le_trans l y h; lt_not_le h l in
6604
{qed}
6605
empty_dom fm
6606
end else begin
6607
{proof…} total l h; {qed}
6608
interval fm l h
6609
end
6610
| _ ->
6611
{proof…}
6612
let lemma no (x y: t) : unit
6613
requires { in_iv y lo hi /\ in_iv x c d /\ in_iv (fp_div m x y) a b }
6614
ensures { false }
6615
= cs x y in
6616
{qed}
6617
empty_dom fm
6618
end
6619
end

The y with x / y in [a, b] for some x of [c, d]

  Valid (2.8s) (colibri2 61) (cvc4 8) (cvc5 44) (split_vc 5) (depth 4)
6622
let
div_back_den_iv
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b c d: t) : dom
6623
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6624
requires { specials4 fm minf pinf nz pz }
6625
requires { fmt a = fm /\ fmt b = fm /\ fmt c = fm /\ fmt d = fm }
6626
requires { le_dz a b /\ le_dz c d }
6627
ensures { result.fm = fm }
6628
ensures { forall x y. fmt y = fm -> is_not_nan y -> in_iv x c d ->
6629
in_iv (fp_div m x y) a b -> mem y result }
6630
= {proof…}
6631
le_not_nan a b; le_not_nan c d; format_facts fm ();
6632
index_range minf; index_range pinf; index_range nz; index_range pz;
6633
of_index_range fm 1; of_index_range fm (-2);
6634
of_index_range fm (nb_sign fm - 2); of_index_range fm (1 - nb_sign fm);
6635
succ_gt pz; pred_lt pinf; succ_gt minf; pred_lt nz;
6636
{qed}
6637
let psub = succ_diff_zero pz in
6638
let pmax = pred_diff_zero pinf in
6639
let nmin = succ_diff_zero minf in
6640
let nmax = pred_diff_zero nz in
6641
{proof…}
6642
assert { index psub = 1 /\ index pmax = nb_sign fm - 2 };
6643
assert { index nmin = 1 - nb_sign fm /\ index nmax = -2 };
6644
region_facts fm true pz pinf pz; region_facts fm false minf nz pz;
6645
{qed}
6646
let c1 = max_diff_zero c psub in
6647
let d1 = min_diff_zero d pmax in
6648
let fp =
6649
if le_diff_zero c1 d1 then begin
6650
{proof…} index_range c1; index_range d1; {qed}
6651
union fm (div_back_den_fin fm m true pz pinf minf nz true a b c1 d1)
6652
(div_back_den_fin fm m false minf nz pz pinf true a b c1 d1)
6653
end else empty_dom fm in
6654
let c2 = max_diff_zero c nmin in
6655
let d2 = min_diff_zero d nmax in
6656
let fn =
6657
if le_diff_zero c2 d2 then begin
6658
{proof…} index_range c2; index_range d2; {qed}
6659
union fm (div_back_den_fin fm m true pz pinf minf nz false a b c2 d2)
6660
(div_back_den_fin fm m false minf nz pz pinf false a b c2 d2)
6661
end else empty_dom fm in
6662
(* the zeros: a nonzero y gives a zero *)
6663
let zn = in_ivf nz a b in
6664
let zp = in_ivf pz a b in
6665
let z1 = if in_ivf nz c d then union fm (iv_if fm zn psub pinf) (iv_if fm zp minf nmax)
6666
else empty_dom fm in
6667
let z2 = if in_ivf pz c d then union fm (iv_if fm zp psub pinf) (iv_if fm zn minf nmax)
6668
else empty_dom fm in
6669
(* the infinities: a finite y gives an infinity *)
6670
let im = in_ivf minf a b in
6671
let ip = in_ivf pinf a b in
6672
let i1 = if in_ivf minf c d then union fm (iv_if fm im pz pmax) (iv_if fm ip nmin nz)
6673
else empty_dom fm in
6674
let i2 = if in_ivf pinf c d then union fm (iv_if fm ip pz pmax) (iv_if fm im nmin nz)
6675
else empty_dom fm in
6676
let fpn = union fm fp fn in
6677
let zs = union fm z1 z2 in
6678
let zi = union fm i1 i2 in
6679
let res = union fm fpn (union fm zs zi) in
6680
{proof…}
6681
let lemma pt (x y: t) : unit
6682
requires { fmt y = fm /\ is_not_nan y /\ in_iv x c d /\ in_iv (fp_div m x y) a b }
6683
ensures { mem y res }
6684
= le_not_nan c x; le_not_nan a (fp_div m x y);
6685
index_range x; index_range y;
6686
region_facts fm true pz pinf y; region_facts fm false minf nz y;
6687
div_special m x y;
6688
if pure { is_zero x } then begin
6689
if pure { is_positive x } then
6690
assert { x = pz by of_index fm (index x) = of_index fm (index pz) }
6691
else
6692
assert { x = nz by of_index fm (index x) = of_index fm (index nz) };
6693
assert { not is_zero y };
6694
assert { is_positive y -> in_iv y psub pinf };
6695
assert { not is_positive y -> in_iv y minf nmax };
6696
let r = fp_div m x y in
6697
index_range r;
6698
if pure { is_positive r } then
6699
assert { r = pz by of_index fm (index r) = of_index fm (index pz) }
6700
else
6701
assert { r = nz by of_index fm (index r) = of_index fm (index nz) };
6702
assert { mem y zs }
6703
end else if pure { is_infinity x } then begin
6704
if pure { is_plus_infinity x } then plus_infinity_unique x
6705
else minus_infinity_unique x;
6706
assert { not is_infinity y };
6707
assert { is_positive y -> in_iv y pz pmax };
6708
assert { not is_positive y -> in_iv y nmin nz };
6709
let r = fp_div m x y in
6710
if pure { is_plus_infinity r } then plus_infinity_unique r
6711
else minus_infinity_unique r;
6712
assert { mem y zi }
6713
end else if pure { is_positive x } then begin
6714
assert { fmt x = fm /\ 1 <= index x <= nb_sign fm - 2 };
6715
assert { le_dz psub x /\ le_dz x pmax };
6716
assert { in_iv x c1 d1 };
6717
le_trans c1 x d1;
6718
assert { le_dz c1 d1 };
6719
assert { is_positive y -> in_iv y pz pinf };
6720
assert { not is_positive y -> in_iv y minf nz };
6721
assert { mem y fp };
6722
assert { mem y fpn }
6723
end else begin
6724
assert { fmt x = fm /\ 1 - nb_sign fm <= index x <= -2 };
6725
assert { le_dz nmin x /\ le_dz x nmax };
6726
assert { in_iv x c2 d2 };
6727
le_trans c2 x d2;
6728
assert { le_dz c2 d2 };
6729
assert { is_positive y -> in_iv y pz pinf };
6730
assert { not is_positive y -> in_iv y minf nz };
6731
assert { mem y fn };
6732
assert { mem y fpn }
6733
end in
6734
{qed}
6735
res
6736
Valid (466ms) (cvc5)
6737
let rec
div_back_den_iv_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (a b: t)
6738
(l: list (t, t)) : dom
6739
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6740
requires { specials4 fm minf pinf nz pz }
6741
requires { fmt a = fm /\ fmt b = fm /\ le_dz a b /\ ok_u l /\ fmt_u fm l }
6742
ensures { result.fm = fm }
6743
ensures { forall x y. fmt y = fm -> is_not_nan y -> mem_u x l ->
6744
in_iv (fp_div m x y) a b -> mem y result }
6745
variant { l }
6746
= match l with
6747
| Nil -> empty_dom fm
6748
| Cons (c, d) r ->
6749
union fm (div_back_den_iv fm minf pinf nz pz m a b c d)
6750
(div_back_den_iv_u fm minf pinf nz pz m a b r)
6751
end
6752
Valid (502ms) (cvc5)
6753
let rec
div_back_den_u
(ghost fm: format) (minf pinf nz pz: t) (m: mode)
6754
(l1 l2: list (t, t)) : dom
6755
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6756
requires { specials4 fm minf pinf nz pz }
6757
requires { ok_u l1 /\ fmt_u fm l1 /\ ok_u l2 /\ fmt_u fm l2 }
6758
ensures { result.fm = fm }
6759
ensures { forall x y. fmt y = fm -> is_not_nan y -> mem_u x l2 ->
6760
mem_u (fp_div m x y) l1 -> mem y result }
6761
variant { l1 }
6762
= match l1 with
6763
| Nil -> empty_dom fm
6764
| Cons (a, b) r ->
6765
union fm (div_back_den_iv_u fm minf pinf nz pz m a b l2)
6766
(div_back_den_u fm minf pinf nz pz m r l2)
6767
end

The y such that x / y is in r for some x in n

  Valid (1.0s) (colibri2 28) (cvc4) (cvc5 7) (split_vc) (depth 1)
6770
let
div_back_den
(ghost fm: format) (minf pinf nz pz: t) (m: mode) (r n: dom) : dom
6771
requires { fmt minf = fm /\ fmt pinf = fm /\ fmt nz = fm /\ fmt pz = fm }
6772
requires { specials4 fm minf pinf nz pz }
6773
requires { r.fm = fm /\ n.fm = fm }
6774
ensures { result.fm = fm }
6775
ensures { forall x y. fmt y = fm -> mem x n -> mem (fp_div m x y) r -> mem y result }
6776
= {proof…}
6777
index_range minf; index_range pinf; index_range nz; index_range pz;
6778
{qed}
6779
let u = div_back_den_u fm minf pinf nz pz m r.u n.u in
6780
let nn =
6781
if r.nan then
6782
if n.nan then top fm minf pinf
6783
else begin
6784
let p = if meets fm nz pz n
6785
then union fm (singleton fm nz) (singleton fm pz) else empty_dom fm in
6786
let q = if meets fm minf minf n || meets fm pinf pinf n
6787
then union fm (singleton fm minf) (singleton fm pinf) else empty_dom fm in
6788
union fm (nan_dom fm) (union fm p q)
6789
end
6790
else empty_dom fm in
6791
let res = union fm nn u in
6792
{proof…}
6793
let lemma pt (x y: t) : unit
6794
requires { fmt y = fm /\ mem x n /\ mem (fp_div m x y) r }
6795
ensures { mem y res }
6796
= div_special m x y; mul_div_fmt m x y;
6797
if pure { is_nan (fp_div m x y) } then begin
6798
if pure { is_not_nan x /\ is_not_nan y } then begin
6799
index_range x; index_range y;
6800
if pure { is_zero y } then begin
6801
if pure { is_positive y } then
6802
assert { y = pz by of_index fm (index y) = of_index fm (index pz) }
6803
else
6804
assert { y = nz by of_index fm (index y) = of_index fm (index nz) };
6805
assert { index nz <= index x <= index pz }
6806
end else begin
6807
if pure { is_plus_infinity y } then plus_infinity_unique y
6808
else minus_infinity_unique y;
6809
if pure { is_plus_infinity x } then plus_infinity_unique x
6810
else minus_infinity_unique x
6811
end
6812
end
6813
end in
6814
{qed}
6815
res
6816
end