123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178(************************************************************************)(* * The Coq Proof Assistant / The Coq Development Team *)(* v * Copyright INRIA, CNRS and contributors *)(* <O___,, * (see version control and CREDITS file for authors & dates) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)(** [Big] : a wrapper around ocaml [ZArith] with nicer names,
and a few extraction-specific constructions *)(** To be linked with [zarith] *)typebig_int=Z.t(** The type of big integers. *)letzero=Z.zero(** The big integer [0]. *)letone=Z.one(** The big integer [1]. *)lettwo=Z.of_int2(** The big integer [2]. *)(** {6 Arithmetic operations} *)letopp=Z.neg(** Unary negation. *)letabs=Z.abs(** Absolute value. *)letadd=Z.add(** Addition. *)letsucc=Z.succ(** Successor (add 1). *)letadd_int=Z.add(** Addition of a small integer to a big integer. *)letsub=Z.sub(** Subtraction. *)letpred=Z.pred(** Predecessor (subtract 1). *)letmult=Z.mul(** Multiplication of two big integers. *)letmult_intxy=Z.mul(Z.of_intx)y(** Multiplication of a big integer by a small integer *)letsquarex=Z.mulxx(** Return the square of the given big integer *)letsqrt=Z.sqrt(** [sqrt_big_int a] returns the integer square root of [a],
that is, the largest big integer [r] such that [r * r <= a].
Raise [Invalid_argument] if [a] is negative. *)letquomod=Z.div_rem(** Euclidean division of two big integers.
The first part of the result is the quotient,
the second part is the remainder.
Writing [(q,r) = quomod_big_int a b], we have
[a = q * b + r] and [0 <= r < |b|].
Raise [Division_by_zero] if the divisor is zero. *)letdiv=Z.div(** Euclidean quotient of two big integers.
This is the first result [q] of [quomod_big_int] (see above). *)letmodulo=Z.(mod)(** Euclidean modulus of two big integers.
This is the second result [r] of [quomod_big_int] (see above). *)letgcd=Z.gcd(** Greatest common divisor of two big integers. *)letpower=Z.pow(** Exponentiation functions. Return the big integer
representing the first argument [a] raised to the power [b]
(the second argument). Depending
on the function, [a] and [b] can be either small integers
or big integers. Raise [Invalid_argument] if [b] is negative. *)(** {6 Comparisons and tests} *)letsign=Z.sign(** Return [0] if the given big integer is zero,
[1] if it is positive, and [-1] if it is negative. *)letcompare=Z.compare(** [compare_big_int a b] returns [0] if [a] and [b] are equal,
[1] if [a] is greater than [b], and [-1] if [a] is smaller
than [b]. *)leteq=Z.equalletle=Z.leqletge=Z.geqletlt=Z.ltletgt=Z.gt(** Usual boolean comparisons between two big integers. *)letmax=Z.max(** Return the greater of its two arguments. *)letmin=Z.min(** Return the smaller of its two arguments. *)(** {6 Conversions to and from strings} *)letto_string=Z.to_string(** Return the string representation of the given big integer,
in decimal (base 10). *)letof_string=Z.of_string(** Convert a string to a big integer, in decimal.
The string consists of an optional [-] or [+] sign,
followed by one or several decimal digits. *)(** {6 Conversions to and from other numerical types} *)letof_int=Z.of_int(** Convert a small integer to a big integer. *)letis_int=Z.fits_int(** Test whether the given big integer is small enough to
be representable as a small integer (type [int])
without loss of precision. On a 32-bit platform,
[is_int_big_int a] returns [true] if and only if
[a] is between 2{^30} and 2{^30}-1. On a 64-bit platform,
[is_int_big_int a] returns [true] if and only if
[a] is between -2{^62} and 2{^62}-1. *)letto_int=Z.to_int(** Convert a big integer to a small integer (type [int]).
Raises [Failure "int_of_big_int"] if the big integer
is not representable as a small integer. *)(** Functions used by extraction *)letdoublex=mult_int2xletdoubleplusonex=succ(doublex)letnat_casefOfSn=ifsignn<=0thenfO()elsefS(predn)letpositive_casef2p1f2pf1p=ifleponethenf1()elselet(q,r)=quomodptwoinifeqrzerothenf2pqelsef2p1qletn_casefOfpn=ifsignn<=0thenfO()elsefpnletz_casefOfpfnz=lets=signzinifs=0thenfO()elseifs>0thenfpzelsefn(oppz)letcompare_caseelgxy=lets=comparexyinifs=0theneelseifs<0thenlelsegletnat_recfOfS=letrecloopaccn=ifsignn<=0thenaccelseloop(fSacc)(predn)inloopfOletpositive_recf2p1f2pf1=letrecloopn=iflenonethenf1elselet(q,r)=quomodntwoinifeqrzerothenf2p(loopq)elsef2p1(loopq)inloopletz_recfOfpfn=z_case(fun_->fO)fpfn