| 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 | open Mutils |
| 12 | open NumCompat |
| 13 | module Mc = Micromega |
| 14 | |
| 15 | val max_nb_cstr : int ref |
| 16 | |
| 17 | type var = int |
| 18 | |
| 19 | module Monomial : sig |
| 20 | (** A monomial is represented by a multiset of variables *) |
| 21 | type t |
| 22 | |
| 23 | (** [degree m] is the sum of the degrees of each variable *) |
| 24 | val degree : t -> int |
| 25 | |
| 26 | (** [subset m1 m2] holds if the multi-set [m1] is included in [m2] *) |
| 27 | val subset : t -> t -> bool |
| 28 | |
| 29 | (** [fold f m acc] folds f over the multiset m *) |
| 30 | val fold : (var -> int -> 'a -> 'a) -> t -> 'a -> 'a |
| 31 | |
| 32 | (** [output o m] outputs a textual representation *) |
| 33 | val output : out_channel -> t -> unit |
| 34 | |
| 35 | end |
| 36 | |
| 37 | module MonMap : sig |
| 38 | include Map.S with type key = Monomial.t |
| 39 | |
| 40 | val union : (Monomial.t -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t |
| 41 | end |
| 42 | |
| 43 | module Poly : sig |
| 44 | (** Representation of polonomial with rational coefficient. |
| 45 | a1.m1 + ... + c where |
| 46 | - ai are rational constants (num type) |
| 47 | - mi are monomials |
| 48 | - c is a rational constant |
| 49 | |
| 50 | *) |
| 51 | |
| 52 | type t |
| 53 | |
| 54 | (** [constant c] |
| 55 | @return the constant polynomial c *) |
| 56 | val constant : Q.t -> t |
| 57 | |
| 58 | (** [variable x] |
| 59 | @return the polynomial 1.x^1 *) |
| 60 | val variable : var -> t |
| 61 | |
| 62 | (** [addition p1 p2] |
| 63 | @return the polynomial p1+p2 *) |
| 64 | val addition : t -> t -> t |
| 65 | |
| 66 | (** [product p1 p2] |
| 67 | @return the polynomial p1*p2 *) |
| 68 | val product : t -> t -> t |
| 69 | |
| 70 | (** [uminus p] |
| 71 | @return the polynomial -p i.e product by -1 *) |
| 72 | val uminus : t -> t |
| 73 | |
| 74 | (** [get mi p] |
| 75 | @return the coefficient ai of the monomial mi. *) |
| 76 | val get : Monomial.t -> t -> Q.t |
| 77 | |
| 78 | (** [fold f p a] folds f over the monomials of p with non-zero coefficient *) |
| 79 | val fold : (Monomial.t -> Q.t -> 'a -> 'a) -> t -> 'a -> 'a |
| 80 | |
| 81 | (** [add m n p] |
| 82 | @return the polynomial n*m + p *) |
| 83 | val add : Monomial.t -> Q.t -> t -> t |
| 84 | end |
| 85 | |
| 86 | type cstr = {coeffs : Vect.t; op : op; cst : Q.t} |
| 87 | |
| 88 | (* Representation of linear constraints *) |
| 89 | and op = Eq | Ge | Gt |
| 90 | |
| 91 | val eval_op : op -> Q.t -> Q.t -> bool |
| 92 | val compare_op : op -> op -> int |
| 93 | |
| 94 | val opAdd : op -> op -> op |
| 95 | |
| 96 | (** [is_strict c] |
| 97 | @return whether the constraint is strict i.e. c.op = Gt *) |
| 98 | val is_strict : cstr -> bool |
| 99 | |
| 100 | exception Strict |
| 101 | |
| 102 | module LinPoly : sig |
| 103 | (** Linear(ised) polynomials represented as a [Vect.t] |
| 104 | i.e a sorted association list. |
| 105 | The constant is the coefficient of the variable 0 |
| 106 | |
| 107 | Each linear polynomial can be interpreted as a multi-variate polynomial. |
| 108 | There is a bijection mapping between a linear variable and a monomial |
| 109 | (see module [MonT]) |
| 110 | *) |
| 111 | |
| 112 | type t = Vect.t |
| 113 | |
| 114 | (** Each variable of a linear polynomial is mapped to a monomial. |
| 115 | This is done using the monomial tables of the module MonT. *) |
| 116 | |
| 117 | module MonT : sig |
| 118 | (** [clear ()] clears the mapping. *) |
| 119 | val clear : unit -> unit |
| 120 | |
| 121 | (** [reserve i] reserves the integer i *) |
| 122 | val reserve : int -> unit |
| 123 | |
| 124 | (** [safe_reserve i] reserves the integer i *) |
| 125 | val safe_reserve : int -> unit |
| 126 | |
| 127 | (** [get_fresh ()] return the first fresh variable *) |
| 128 | val get_fresh : unit -> int |
| 129 | |
| 130 | (** [retrieve x] |
| 131 | @return the monomial corresponding to the variable [x] *) |
| 132 | val retrieve : int -> Monomial.t |
| 133 | |
| 134 | (** [register m] |
| 135 | @return the variable index for the monomial m *) |
| 136 | val register : Monomial.t -> int |
| 137 | end |
| 138 | |
| 139 | (** [linpol_of_pol p] linearise the polynomial p *) |
| 140 | val linpol_of_pol : Poly.t -> t |
| 141 | |
| 142 | (** [var x] |
| 143 | @return 1.y where y is the variable index of the monomial x^1. |
| 144 | *) |
| 145 | val var : var -> t |
| 146 | |
| 147 | (** [of_monomial m] |
| 148 | @returns 1.x where x is the variable (index) for monomial m *) |
| 149 | val of_monomial : Monomial.t -> t |
| 150 | |
| 151 | (** [of_vect v] |
| 152 | @returns a1.x1 + ... + an.xn |
| 153 | This is not the identity because xi is the variable index of xi^1 |
| 154 | *) |
| 155 | val of_vect : Vect.t -> t |
| 156 | |
| 157 | (** [variables p] |
| 158 | @return the set of variables of the polynomial p |
| 159 | interpreted as a multi-variate polynomial *) |
| 160 | val variables : t -> ISet.t |
| 161 | |
| 162 | (** [is_variable p] |
| 163 | @return Some x if p = a.x for a >= 0 *) |
| 164 | val is_variable : t -> var option |
| 165 | |
| 166 | (** [is_linear p] |
| 167 | @return whether the multi-variate polynomial is linear. *) |
| 168 | val is_linear : t -> bool |
| 169 | |
| 170 | (** [is_linear_for x p] |
| 171 | @return true if the polynomial is linear in x |
| 172 | i.e can be written c*x+r where c is a constant and r is independent from x *) |
| 173 | val is_linear_for : var -> t -> bool |
| 174 | |
| 175 | (** [constant c] |
| 176 | @return the constant polynomial c |
| 177 | *) |
| 178 | val constant : Q.t -> t |
| 179 | |
| 180 | (** [search_all_linear pred p] |
| 181 | @return all the variables x such p = a.x + b such that |
| 182 | p is linear in x i.e x does not occur in b and |
| 183 | a is a constant such that [pred a] *) |
| 184 | val search_all_linear : (Q.t -> bool) -> t -> var list |
| 185 | |
| 186 | (** [product p q] |
| 187 | @return the product of the polynomial [p*q] *) |
| 188 | val product : t -> t -> t |
| 189 | |
| 190 | (** [collect_square p] |
| 191 | @return a mapping m such that m[s] = s^2 |
| 192 | for every s^2 that is a monomial of [p] *) |
| 193 | val collect_square : t -> Monomial.t MonMap.t |
| 194 | |
| 195 | (** [monomials p] |
| 196 | @return the set of monomials. *) |
| 197 | val monomials : t -> ISet.t |
| 198 | |
| 199 | (** [pp_var o v] pretty-prints a monomial indexed by v. *) |
| 200 | val pp_var : out_channel -> var -> unit |
| 201 | |
| 202 | (** [pp o p] pretty-prints a polynomial. *) |
| 203 | val pp : out_channel -> t -> unit |
| 204 | |
| 205 | (** [pp_goal typ o l] pretty-prints the list of constraints as a Rocq goal. *) |
| 206 | val pp_goal : string -> out_channel -> (t * op) list -> unit |
| 207 | end |
| 208 | |
| 209 | module ProofFormat : sig |
| 210 | (** Proof format used by the proof-generating procedures. |
| 211 | It is fairly close to Rocq format but a bit more liberal. |
| 212 | |
| 213 | It is used for proofs over Z, Q, R. |
| 214 | However, certain constructions e.g. [CutPrf] are only relevant for Z. |
| 215 | *) |
| 216 | |
| 217 | type prf_rule = |
| 218 | | Annot of string * prf_rule |
| 219 | | Hyp of int |
| 220 | | Def of int |
| 221 | | Ref of int |
| 222 | | Cst of Q.t |
| 223 | | Zero |
| 224 | | Square of Vect.t |
| 225 | | MulC of Vect.t * prf_rule |
| 226 | | Gcd of Z.t * prf_rule |
| 227 | | MulPrf of prf_rule * prf_rule |
| 228 | | AddPrf of prf_rule * prf_rule |
| 229 | | CutPrf of prf_rule |
| 230 | | LetPrf of prf_rule * prf_rule |
| 231 | |
| 232 | type proof = |
| 233 | | Done |
| 234 | | Step of int * prf_rule * proof |
| 235 | | Split of int * Vect.t * proof * proof |
| 236 | | Enum of int * prf_rule * Vect.t * prf_rule * proof list |
| 237 | | ExProof of int * int * int * var * var * var * proof |
| 238 | |
| 239 | (* x = z - t, z >= 0, t >= 0 *) |
| 240 | |
| 241 | val pr_size : prf_rule -> Q.t |
| 242 | val normalise_proof : int -> proof -> int * proof |
| 243 | val output_prf_rule : out_channel -> prf_rule -> unit |
| 244 | val output_proof : out_channel -> proof -> unit |
| 245 | val add_proof : prf_rule -> prf_rule -> prf_rule |
| 246 | val mul_cst_proof : Q.t -> prf_rule -> prf_rule |
| 247 | val mul_proof : prf_rule -> prf_rule -> prf_rule |
| 248 | |
| 249 | module Env: sig |
| 250 | type t |
| 251 | val make : int -> t |
| 252 | end |
| 253 | |
| 254 | val compile_proof : Env.t -> proof -> Micromega.zArithProof |
| 255 | |
| 256 | val cmpl_prf_rule : |
| 257 | ('a Micromega.pExpr -> 'a Micromega.pol) |
| 258 | -> (Q.t -> 'a) |
| 259 | -> Env.t |
| 260 | -> prf_rule |
| 261 | -> 'a Micromega.psatz |
| 262 | |
| 263 | val proof_of_farkas : prf_rule IMap.t -> Vect.t -> prf_rule |
| 264 | val simplify_proof : proof -> proof * Mutils.ISet.t |
| 265 | |
| 266 | end |
| 267 | |
| 268 | val output_cstr : out_channel -> cstr -> unit |
| 269 | |
| 270 | (** [module WithProof] constructs polynomials packed with the proof that their sign is correct. *) |
| 271 | module WithProof : sig |
| 272 | type t |
| 273 | |
| 274 | (** [InvalidProof] is raised if the operation is invalid. *) |
| 275 | exception InvalidProof |
| 276 | |
| 277 | val repr : t -> (LinPoly.t * op) * ProofFormat.prf_rule |
| 278 | |
| 279 | val proof : t -> ProofFormat.prf_rule |
| 280 | |
| 281 | val polynomial : t -> LinPoly.t |
| 282 | |
| 283 | val compare : t -> t -> int |
| 284 | val annot : string -> t -> t |
| 285 | val of_cstr : cstr * ProofFormat.prf_rule -> t |
| 286 | |
| 287 | (** [out_channel chan c] pretty-prints the constraint [c] over the channel [chan] *) |
| 288 | val output : out_channel -> t -> unit |
| 289 | |
| 290 | val output_sys : out_channel -> t list -> unit |
| 291 | |
| 292 | (** [zero] represents the tautology (0=0) *) |
| 293 | val zero : t |
| 294 | |
| 295 | (** [const n] represents the tautology (n>=0) *) |
| 296 | val const : Q.t -> t |
| 297 | |
| 298 | (** [product p q] |
| 299 | @return the polynomial p*q with its sign and proof *) |
| 300 | val product : t -> t -> t |
| 301 | |
| 302 | (** [addition p q] |
| 303 | @return the polynomial p+q with its sign and proof *) |
| 304 | val addition : t -> t -> t |
| 305 | |
| 306 | (** [neg p] |
| 307 | @return the polynomial -p with its sign and proof |
| 308 | @raise an error if this not an equality |
| 309 | *) |
| 310 | val neg : t -> t |
| 311 | |
| 312 | (** [mul_cst c q] |
| 313 | @return the polynomial c * q with its sign and proof. *) |
| 314 | val mul_cst : Q.t -> t -> t |
| 315 | |
| 316 | (** [def p op i] creates an alias with the variable index [i] *) |
| 317 | val def : LinPoly.t -> op -> int -> t |
| 318 | |
| 319 | (** [square p q] is q = p^2 >= 0 *) |
| 320 | val square : LinPoly.t -> LinPoly.t -> t |
| 321 | |
| 322 | (** [mkhyp p op i] binds p to hypothesis [i] *) |
| 323 | val mkhyp : LinPoly.t -> op -> int -> t |
| 324 | |
| 325 | (** [cutting_plane p] does integer reasoning and adjust the constant to be integral *) |
| 326 | val cutting_plane : t -> t option |
| 327 | |
| 328 | (** [linear_pivot sys p x q] |
| 329 | @return the polynomial [q] where [x] is eliminated using the polynomial [p] |
| 330 | The pivoting operation is only defined if |
| 331 | - p is linear in x i.e p = a.x+b and x neither occurs in a and b |
| 332 | - The pivoting also requires some sign conditions for [a] |
| 333 | *) |
| 334 | val linear_pivot : t list -> t -> Vect.var -> t -> t option |
| 335 | |
| 336 | (** [simple_pivot (c,x) p q] performs a pivoting over the variable [x] where |
| 337 | p = c+a1.x1+....+c.x+...an.xn and c <> 0 *) |
| 338 | val simple_pivot : Q.t * var -> t -> t -> t option |
| 339 | |
| 340 | (** [sort sys] sorts constraints according to the lexicographic order (number of variables, size of the smallest coefficient *) |
| 341 | val sort : t list -> ((int * (Q.t * var)) * t) list |
| 342 | |
| 343 | (** [subst sys] performs the equivalent of the 'subst' tactic of Rocq. |
| 344 | For every p=0 \in sys such that p is linear in x with coefficient +/- 1 |
| 345 | i.e. p = 0 <-> x = e and x \notin e. |
| 346 | Replace x by e in sys |
| 347 | |
| 348 | NB: performing this transformation may hinders the non-linear prover to find a proof. |
| 349 | [elim_simple_linear_equality] is much more careful. |
| 350 | *) |
| 351 | |
| 352 | val subst : t list -> t list |
| 353 | |
| 354 | (** [subst_constant b sys] performs the equivalent of the 'subst' tactic of Rocq |
| 355 | only if there is an equation a.x = c for a,c a constant and a divides c if b= true*) |
| 356 | val subst_constant : bool -> t list -> t list |
| 357 | |
| 358 | val saturate_subst : bool -> t list -> t list |
| 359 | end |
| 360 | |
| 361 | module BoundWithProof : sig |
| 362 | type t |
| 363 | val make : WithProof.t -> t option |
| 364 | val mul_bound : t -> t -> t option |
| 365 | val bound : t -> Vect.Bound.t |
| 366 | val proof : t -> WithProof.t |
| 367 | end |