Common — library colibrilib.stda — module Export

The functions without precondition, for the extraction: a division by zero raises Division_by_zero, the square root of a negative number Negative, and to_z of a number that is not an integer NotInteger

656
module Export
657
use colibrilib.spec.Value
658
use int.Int
659
use real.RealInfix
660
use real.FromInt
661
use real.Square
662
use real.Truncate
663
use colibrilib.zarith.Q as ZQ
664
use colibrilib.stdq.StdQ'impl as SQ
665
use colibrilib.stdq.StdQ as IQ
666
use colibrilib.stdq.Export as SQE
667
use mach.int.Int63
668
use StdA'impl
669
670
use real.PowerInt as RP
671
672
exception
Negative
673
exception
NotInteger
674
Valid (70ms) (colibri2)
675
let
inv
(a:t) : t
676
ensures { value1 real (fun x -> 1. /. x) a result }
677
raises { SQE.DivZero -> real a = 0. }
678
= if is_zero a then raise SQE.DivZero;
679
div (of_int 1) a
680
Valid (65ms) (colibri2)
681
let
div
(a b:t) : t
682
ensures { value2 real (/.) a b result }
683
raises { SQE.DivZero -> real b = 0. }
684
= if is_zero b then raise SQE.DivZero; div a b
685
Valid (61ms) (colibri2)
686
let
div_e
(a b:t) : t
687
ensures { value2 real (fun x y -> from_int (Real.div_e x y)) a b result }
688
raises { SQE.DivZero -> real b = 0. }
689
= if is_zero b then raise SQE.DivZero; div_e a b
690
Valid (60ms) (colibri2)
691
let
div_t
(a b:t) : t
692
ensures { value2 real (fun x y -> from_int (Real.div_t x y)) a b result }
693
raises { SQE.DivZero -> real b = 0. }
694
= if is_zero b then raise SQE.DivZero; div_t a b
695
Valid (58ms) (colibri2)
696
let
div_f
(a b:t) : t
697
ensures { value2 real (fun x y -> from_int (Real.div_f x y)) a b result }
698
raises { SQE.DivZero -> real b = 0. }
699
= if is_zero b then raise SQE.DivZero; div_f a b
700
Valid (54ms) (colibri2)
701
let
mod_e
(a b:t) : t
702
ensures { value2 real Real.mod_e a b result }
703
raises { SQE.DivZero -> real b = 0. }
704
= if is_zero b then raise SQE.DivZero; mod_e a b
705
Valid (55ms) (colibri2)
706
let
mod_t
(a b:t) : t
707
ensures { value2 real Real.mod_t a b result }
708
raises { SQE.DivZero -> real b = 0. }
709
= if is_zero b then raise SQE.DivZero; mod_t a b
710
Valid (58ms) (colibri2)
711
let
mod_f
(a b:t) : t
712
ensures { value2 real Real.mod_f a b result }
713
raises { SQE.DivZero -> real b = 0. }
714
= if is_zero b then raise SQE.DivZero; mod_f a b
715
Valid (64ms) (colibri2)
716
let
sqrt
(a:t) : t
717
ensures { value1 real Square.sqrt a result }
718
raises { Negative -> real a <. 0. }
719
= if sign a < 0 then raise Negative; StdA'impl.sqrt a

a^n for n >= 0; Negative for a negative exponent

  Valid (65ms) (colibri2)
722
let
pow
(a:t) (n:int63) : t
723
ensures { value1 real (fun x -> RP.power x n) a result }
724
raises { Negative -> n < 0 }
725
= if n < 0 then raise Negative; pow a n
726
727
exception
PowDomain

a^e, the non negative root; PowDomain outside of its domain

  Valid (1.5s) (colibri2)
730
let
positive_pow
[@spec:none "a root: the e.den-th power of the result is the e.num-th power of a"] (a:t) (e:ZQ.t) : t
731
ensures { 0. <=. real result }
732
ensures { 0 <= e.ZQ.num ->
733
RP.power (real result) e.ZQ.den = RP.power (real a) e.ZQ.num }
734
ensures { e.ZQ.num < 0 ->
735
RP.power (real result) e.ZQ.den *. RP.power (real a) (- e.ZQ.num) = 1. }
736
raises { PowDomain -> not (0. <. real a \/ (real a = 0. /\ 0 < e.ZQ.num)) }
737
= let s = sign a in
738
if s < 0 || (s = 0 && ZQ.sign e <= 0) then raise PowDomain;
739
positive_pow a (IQ.of_zarith e)

The value of an integer

  Valid (56ms) (colibri2)
742
let
to_z
(a:t) : int
743
ensures { value0 from_int (real a) result }
744
raises { NotInteger -> not (exists z. real a = from_int z) }
745
= if not (is_integer a) then raise NotInteger; to_z a

floor (a / m) * m: for m > 0, the largest multiple of m below a

  Valid (71ms) (colibri2)
749
let
round_down_to
(a:t) (m:ZQ.t) : ZQ.t
750
ensures { value0 ZQ.real (from_int (Truncate.floor (real a /. ZQ.real m)) *. ZQ.real m) result }
751
raises { SQE.DivZero -> ZQ.real m = 0. }
752
= match a with
753
| Q q -> SQE.round_down_to (IQ.to_zarith q) m
754
| A ia ->
755
if SQ.is_zero m then raise SQE.DivZero;
756
let q' = CA.floor (CA.div ia.ca (CA.of_q m)) in
757
SQ.mul (SQ.of_bigint q') m
758
end

ceil (a / m) * m

  Valid (71ms) (colibri2)
761
let
round_up_to
(a:t) (m:ZQ.t) : ZQ.t
762
ensures { value0 ZQ.real (from_int (Truncate.ceil (real a /. ZQ.real m)) *. ZQ.real m) result }
763
raises { SQE.DivZero -> ZQ.real m = 0. }
764
= match a with
765
| Q q -> SQE.round_up_to (IQ.to_zarith q) m
766
| A ia ->
767
if SQ.is_zero m then raise SQE.DivZero;
768
let q' = CA.ceil (CA.div ia.ca (CA.of_q m)) in
769
SQ.mul (SQ.of_bigint q') m
770
end
771
772
use real.Abs as RA
773
use colibrilib.spec.Test
774
Valid (30ms) (colibri2)
775
let
compare_int
(a b:t) : int63
776
ensures { result > 0 <-> real a >. real b }
777
ensures { result < 0 <-> real a <. real b }
778
= StdA'impl.compare_int a b
779
Valid (37ms) (colibri2)
780
let
equal
(a b:t) : bool
781
ensures { decides (real a = real b) result }
782
= StdA'impl.equal a b
783
Valid (32ms) (colibri2)
784
let
sign
(a:t) : int63
785
ensures { result > 0 <-> real a >. 0. }
786
ensures { result < 0 <-> real a <. 0. }
787
= StdA'impl.sign a
788
Valid (34ms) (colibri2)
789
let
is_integer
(a:t) : bool
790
ensures { decides (exists z. real a = from_int z) result }
791
= StdA'impl.is_integer a
792
Valid (28ms) (colibri2)
793
let
of_q
(q:IQ.t) : t
794
ensures { value0 real (IQ.real q) result }
795
= StdA'impl.of_q q
796
Valid (27ms) (colibri2)
797
let
of_bigint
(z:int) : t
798
ensures { value0 real (from_int z) result }
799
= StdA'impl.of_bigint z
800
Valid (36ms) (colibri2)
801
let
of_int
(z:int63) : t
802
ensures { value0 real (from_int z) result }
803
= StdA'impl.of_int z
804
Valid (39ms) (colibri2)
805
let
floor
(a:t) : t
806
ensures { value1 real (fun v -> from_int (Truncate.floor v)) a result }
807
= StdA'impl.floor a
808
Valid (35ms) (colibri2)
809
let
ceil
(a:t) : t
810
ensures { value1 real (fun v -> from_int (Truncate.ceil v)) a result }
811
= StdA'impl.ceil a
812
Valid (26ms) (colibri2)
813
let
floor_z
(a:t) : int
814
ensures { value_is (Truncate.floor (real a)) result }
815
= StdA'impl.floor_z a
816
Valid (29ms) (colibri2)
817
let
ceil_z
(a:t) : int
818
ensures { value_is (Truncate.ceil (real a)) result }
819
= StdA'impl.ceil_z a
820
Valid (31ms) (colibri2)
821
let
truncate_z
(a:t) : int
822
ensures { value_is (Truncate.truncate (real a)) result }
823
= StdA'impl.truncate_z a
824
Valid (22ms) (colibri2)
825
let
truncate
(a:t) : t
826
ensures { value1 real (fun v -> from_int (Truncate.truncate v)) a result }
827
= StdA'impl.truncate a
828
Valid (28ms) (colibri2)
829
let
neg
(a:t) : t
830
ensures { value1 real (fun x -> -. x) a result }
831
= StdA'impl.neg a
832
Valid (28ms) (colibri2)
833
let
abs
(a:t) : t
834
ensures { value1 real RA.abs a result }
835
= StdA'impl.abs a
836
Valid (33ms) (colibri2)
837
let
add
(a b:t) : t
838
ensures { value2 real (+.) a b result }
839
= StdA'impl.add a b
840
Valid (33ms) (colibri2)
841
let
sub
(a b:t) : t
842
ensures { value2 real (-.) a b result }
843
= StdA'impl.sub a b
844
Valid (32ms) (colibri2)
845
let
mul
(a b:t) : t
846
ensures { value2 real (*.) a b result }
847
= StdA'impl.mul a b
848
Valid (35ms) (colibri2)
849
let
is_zero
(a:t) : bool
850
ensures { decides (real a = 0.) result }
851
= StdA'impl.is_zero a
852
Valid (24ms) (colibri2)
853
let
view
(a:t) : view
854
ensures { match result with
855
| Rat q -> real a = IQ.real q
856
| Irr -> forall q:IQ.t. real a <> IQ.real q
857
end }
858
= StdA'impl.view a

Raised when a precision is not positive

861
exception
BadPrec
862
Valid (45ms) (colibri2)
863
let
floor_q
(prec:int) (a:t) : IQ.t
864
ensures { 0 < prec /\ IQ.real result <=. real a <. IQ.real result +. 1. /. from_int prec }
865
raises { BadPrec -> prec <= 0 }
866
= if prec <= 0 then raise BadPrec else StdA'impl.floor_q prec a
867
Valid (39ms) (colibri2)
868
let
ceil_q
(prec:int) (a:t) : IQ.t
869
ensures { 0 < prec /\ IQ.real result -. 1. /. from_int prec <. real a <=. IQ.real result }
870
raises { BadPrec -> prec <= 0 }
871
= if prec <= 0 then raise BadPrec else StdA'impl.ceil_q prec a
872
Valid (42ms) (colibri2)
873
let
round_down_to_small
(bigq: IQ.t -> bool) (max_size_bit prec:int) (x:t) : t
874
ensures { 0 < prec }
875
ensures { match result with Q _ -> true | A _ -> false end }
876
ensures { real result <=. real x }
877
raises { BadPrec -> prec <= 0 }
878
= if prec <= 0 then raise BadPrec else StdA'impl.round_down_to_small bigq max_size_bit prec x
879
Valid (39ms) (colibri2)
880
let
round_up_to_small
(bigq: IQ.t -> bool) (max_size_bit prec:int) (x:t) : t
881
ensures { 0 < prec }
882
ensures { match result with Q _ -> true | A _ -> false end }
883
ensures { real x <=. real result }
884
raises { BadPrec -> prec <= 0 }
885
= if prec <= 0 then raise BadPrec else StdA'impl.round_up_to_small bigq max_size_bit prec x
886
end