Debian Codegraph @

plugins/micromega/numCompat.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module type ZArith = sig
12 type t
13
14 val zero : t
15 val one : t
16 val two : t
17 val add : t -> t -> t
18 val sub : t -> t -> t
19 val mul : t -> t -> t
20 val div : t -> t -> t
21 val neg : t -> t
22 val sign : t -> int
23 val equal : t -> t -> bool
24 val compare : t -> t -> int
25 val power_int : t -> int -> t
26 val quomod : t -> t -> t * t
27 val ppcm : t -> t -> t
28 val gcd : t -> t -> t
29 val lcm : t -> t -> t
30 val to_string : t -> string
31 end
32
33 module Z = struct
34 (* Beware this only works fine in ZArith >= 1.10 due to
35 https://github.com/ocaml/Zarith/issues/58 *)
36 include Z
37
38 (* Constants *)
39 let two = Z.of_int 2
40 let ten = Z.of_int 10
41 let power_int = Big_int_Z.power_big_int_positive_int
42 let quomod = Big_int_Z.quomod_big_int
43
44 (* zarith fails with division by zero if x == 0 && y == 0 *)
45 let lcm x y = if Z.equal x zero && Z.equal y zero then zero else Z.lcm x y
46
47 let ppcm x y =
48 let g = gcd x y in
49 let x' = Z.div x g in
50 let y' = Z.div y g in
51 Z.mul g (Z.mul x' y')
52 end
53
54 module type QArith = sig
55 module Z : ZArith
56
57 type t
58
59 val of_int : int -> t
60 val zero : t
61 val one : t
62 val two : t
63 val ten : t
64 val minus_one : t
65
66 module Notations : sig
67 val ( // ) : t -> t -> t
68 val ( +/ ) : t -> t -> t
69 val ( -/ ) : t -> t -> t
70 val ( */ ) : t -> t -> t
71 val ( =/ ) : t -> t -> bool
72 val ( <>/ ) : t -> t -> bool
73 val ( >/ ) : t -> t -> bool
74 val ( >=/ ) : t -> t -> bool
75 val ( </ ) : t -> t -> bool
76 val ( <=/ ) : t -> t -> bool
77 end
78
79 val compare : t -> t -> int
80 val make : Z.t -> Z.t -> t
81 val den : t -> Z.t
82 val num : t -> Z.t
83 val of_bigint : Z.t -> t
84 val to_bigint : t -> Z.t
85 val neg : t -> t
86
87 (* val inv : t -> t *)
88 val max : t -> t -> t
89 val min : t -> t -> t
90 val sign : t -> int
91 val abs : t -> t
92 val mod_ : t -> t -> t
93 val floor : t -> t
94
95 (* val floorZ : t -> Z.t *)
96 val ceiling : t -> t
97 val round : t -> t
98 val pow2 : int -> t
99 val pow10 : int -> t
100 val power : int -> t -> t
101 val to_string : t -> string
102 val of_string : string -> t
103 val to_float : t -> float
104 end
105
106 module Q : QArith with module Z = Z = struct
107 module Z = Z
108
109 let pow_check_exp x y =
110 let z_res =
111 if y = 0 then Z.one
112 else if y > 0 then Z.pow x y
113 else (* s < 0 *)
114 Z.pow x (abs y)
115 in
116 let z_res = Q.of_bigint z_res in
117 if 0 <= y then z_res else Q.inv z_res
118
119 include Q
120
121 let two = Q.(of_int 2)
122 let ten = Q.(of_int 10)
123
124 module Notations = struct
125 let ( // ) = Q.div
126 let ( +/ ) = Q.add
127 let ( -/ ) = Q.sub
128 let ( */ ) = Q.mul
129 let ( =/ ) = Q.equal
130 let ( <>/ ) x y = not (Q.equal x y)
131 let ( >/ ) = Q.gt
132 let ( >=/ ) = Q.geq
133 let ( </ ) = Q.lt
134 let ( <=/ ) = Q.leq
135 end
136
137 (* XXX: review / improve *)
138 let floorZ q : Z.t = Z.fdiv (num q) (den q)
139 let floor q : t = floorZ q |> Q.of_bigint
140 let ceiling q : t = Z.cdiv (Q.num q) (Q.den q) |> Q.of_bigint
141 let half = Q.make Z.one Z.two
142
143 (* We imitate Num's round which is to the nearest *)
144 let round q = floor (Q.add half q)
145
146 (* XXX: review / improve *)
147 let quo x y =
148 let s = sign y in
149 let res = floor (x / abs y) in
150 if Int.equal s (-1) then neg res else res
151
152 let mod_ x y = x - (y * quo x y)
153
154 (* XXX: review / improve *)
155 (* Note that Z.pow doesn't support negative exponents *)
156
157 let pow2 y = pow_check_exp Z.two y
158 let pow10 y = pow_check_exp Z.ten y
159
160 let power (x : int) (y : t) : t =
161 let y =
162 try Q.to_int y
163 with Z.Overflow ->
164 (* XXX: make doesn't link Pp / CErrors for csdpcert, that could be fixed *)
165 raise (Invalid_argument "[micromega] overflow in exponentiation")
166 (* CErrors.user_err (Pp.str "[micromega] overflow in exponentiation") *)
167 in
168 pow_check_exp (Z.of_int x) y
169 end