| 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 NumCompat |
| 12 | open Mutils |
| 13 | |
| 14 | (** Variables are simply (positive) integers. *) |
| 15 | type var = int |
| 16 | |
| 17 | (** The type of vectors or equivalently linear expressions. |
| 18 | The current implementation is using association lists. |
| 19 | A list [(0,c),(x1,ai),...,(xn,an)] represents the linear expression |
| 20 | c + a1.xn + ... an.xn where ai are rational constants and xi are variables. |
| 21 | |
| 22 | Note that the variable 0 has a special meaning and represent a constant. |
| 23 | Moreover, the representation is spare and variables with a zero coefficient |
| 24 | are not represented. |
| 25 | *) |
| 26 | type t |
| 27 | |
| 28 | type vector = t |
| 29 | |
| 30 | (** {1 Generic functions} *) |
| 31 | |
| 32 | (** [hash] [equal] and [compare] so that Vect.t can be used as |
| 33 | keys for Set Map and Hashtbl *) |
| 34 | |
| 35 | val hash : t -> int |
| 36 | val equal : t -> t -> bool |
| 37 | val compare : t -> t -> int |
| 38 | |
| 39 | (** {1 Basic accessors and utility functions} *) |
| 40 | |
| 41 | (** [pp_gen pp_var o v] prints the representation of the vector [v] over the channel [o] *) |
| 42 | val pp_gen : (out_channel -> var -> unit) -> out_channel -> t -> unit |
| 43 | |
| 44 | (** [pp o v] prints the representation of the vector [v] over the channel [o] *) |
| 45 | val pp : out_channel -> t -> unit |
| 46 | |
| 47 | (** [pp_smt o v] prints the representation of the vector [v] over the channel [o] using SMTLIB conventions *) |
| 48 | val pp_smt : out_channel -> t -> unit |
| 49 | |
| 50 | (** [variables v] returns the set of variables with non-zero coefficients *) |
| 51 | val variables : t -> ISet.t |
| 52 | |
| 53 | (** [get_cst v] returns c i.e. the coefficient of the variable zero *) |
| 54 | val get_cst : t -> Q.t |
| 55 | |
| 56 | (** [decomp_cst v] returns the pair (c,a1.x1+...+an.xn) *) |
| 57 | val decomp_cst : t -> Q.t * t |
| 58 | |
| 59 | val decomp_fst : t -> (var * Q.t) * t |
| 60 | |
| 61 | (** [cst c] returns the vector v=c+0.x1+...+0.xn *) |
| 62 | val cst : Q.t -> t |
| 63 | |
| 64 | (** [is_constant v] holds if [v] is a constant vector i.e. v=c+0.x1+...+0.xn |
| 65 | *) |
| 66 | val is_constant : t -> bool |
| 67 | |
| 68 | (** [null] is the empty vector i.e. 0+0.x1+...+0.xn *) |
| 69 | val null : t |
| 70 | |
| 71 | (** [is_null v] returns whether [v] is the [null] vector i.e [equal v null] *) |
| 72 | val is_null : t -> bool |
| 73 | |
| 74 | (** [get xi v] returns the coefficient ai of the variable [xi]. |
| 75 | [get] is also defined for the variable 0 *) |
| 76 | val get : var -> t -> Q.t |
| 77 | |
| 78 | (** [set xi ai' v] returns the vector c+a1.x1+...ai'.xi+...+an.xn |
| 79 | i.e. the coefficient of the variable xi is set to ai' *) |
| 80 | val set : var -> Q.t -> t -> t |
| 81 | |
| 82 | (** [update xi f v] returns c+a1.x1+...+f(ai).xi+...+an.xn *) |
| 83 | val update : var -> (Q.t -> Q.t) -> t -> t |
| 84 | |
| 85 | (** [fresh v] return the fresh variable with index 1+ max (variables v) *) |
| 86 | val fresh : t -> int |
| 87 | |
| 88 | (** [choose v] decomposes a vector [v] depending on whether it is [null] or not. |
| 89 | @return None if v is [null] |
| 90 | @return Some(x,n,r) where v = r + n.x x is the smallest variable with non-zero coefficient n <> 0. |
| 91 | *) |
| 92 | val choose : t -> (var * Q.t * t) option |
| 93 | |
| 94 | (** [from_list l] returns the vector c+a1.x1...an.xn from the list of coefficient [l=c;a1;...;an] *) |
| 95 | val from_list : Q.t list -> t |
| 96 | |
| 97 | (** [decr_var i v] decrements the variables of the vector [v] by the amount [i]. |
| 98 | Beware, it is only defined if all the variables of v are greater than i |
| 99 | *) |
| 100 | val decr_var : int -> t -> t |
| 101 | |
| 102 | (** [incr_var i v] increments the variables of the vector [v] by the amount [i]. |
| 103 | *) |
| 104 | val incr_var : int -> t -> t |
| 105 | |
| 106 | (** [gcd v] returns gcd(num(c),num(a1),...,num(an)) where num extracts |
| 107 | the numerator of a rational value. *) |
| 108 | val gcd : t -> Z.t |
| 109 | |
| 110 | (** [normalise v] returns a vector with only integer coefficients *) |
| 111 | val normalise : t -> t |
| 112 | |
| 113 | (** {1 Linear arithmetics} *) |
| 114 | |
| 115 | (** [add v1 v2] is vector addition. |
| 116 | @param v1 is of the form c +a1.x1 +...+an.xn |
| 117 | @param v2 is of the form c'+a1'.x1 +...+an'.xn |
| 118 | @return c1+c1'+ (a1+a1').x1 + ... + (an+an').xn |
| 119 | *) |
| 120 | val add : t -> t -> t |
| 121 | |
| 122 | (** [mul a v] is vector multiplication of vector [v] by a scalar [a]. |
| 123 | @return a.v = a.c+a.a1.x1+...+a.an.xn *) |
| 124 | val mul : Q.t -> t -> t |
| 125 | |
| 126 | (** [mul_add c1 v1 c2 v2] returns the linear combination c1.v1+c2.v2 *) |
| 127 | val mul_add : Q.t -> t -> Q.t -> t -> t |
| 128 | |
| 129 | (** [subst x v v'] replaces x by v in vector v' *) |
| 130 | val subst : int -> t -> t -> t |
| 131 | |
| 132 | (** [div c1 v1] returns the mutiplication by the inverse of c1 i.e (1/c1).v1 *) |
| 133 | val div : Q.t -> t -> t |
| 134 | |
| 135 | (** [uminus v] |
| 136 | @return -v the opposite vector of v i.e. (-1).v *) |
| 137 | val uminus : t -> t |
| 138 | |
| 139 | (** {1 Iterators} *) |
| 140 | |
| 141 | (** [fold f acc v] returns f (f (f acc 0 c ) x1 a1 ) ... xn an *) |
| 142 | val fold : ('acc -> var -> Q.t -> 'acc) -> 'acc -> t -> 'acc |
| 143 | |
| 144 | (** [find f v] returns the first [f xi ai] such that [f xi ai <> None]. |
| 145 | If no such xi ai exists, it returns None *) |
| 146 | val find : (var -> Q.t -> 'c option) -> t -> 'c option |
| 147 | |
| 148 | (** [for_all p v] returns /\_{i>=0} (f xi ai) *) |
| 149 | val for_all : (var -> Q.t -> bool) -> t -> bool |
| 150 | |
| 151 | (** [exists2 p v v'] returns Some(xi,ai,ai') |
| 152 | if p(xi,ai,ai') holds and ai,ai' <> 0. |
| 153 | It returns None if no such pair of coefficient exists. *) |
| 154 | val exists2 : (Q.t -> Q.t -> bool) -> t -> t -> (var * Q.t * Q.t) option |
| 155 | |
| 156 | (** [dotproduct v1 v2] is the dot product of v1 and v2. *) |
| 157 | val dotproduct : t -> t -> Q.t |
| 158 | |
| 159 | module Bound : sig |
| 160 | (** represents a0 + ai.xi *) |
| 161 | type t = {cst : Q.t; var : var; coeff : Q.t} |
| 162 | |
| 163 | val of_vect : vector -> t option |
| 164 | val to_vect : t -> vector |
| 165 | end |