123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268# 1 "extract/farith_Big.ml"(************************************************************************)(* v * The Coq Proof Assistant / The Coq Development Team *)(* <O___,, * INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2015 *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(************************************************************************)(** [Big] : a wrapper around ocaml [Big_int] with nicer names,
and a few extraction-specific constructions *)(** To be linked with [nums.(cma|cmxa)] *)openBig_int_Ztypebig_int=Big_int_Z.big_int(* The type of big integers. *)letzero=zero_big_int(* The big integer [0]. *)letone=unit_big_int(* The big integer [1]. *)lettwo=big_int_of_int2(* The big integer [2]. *)(* {6 Arithmetic operations} *)letopp=minus_big_int(* Unary negation. *)letabs=abs_big_int(* Absolute value. *)letadd=add_big_int(* Addition. *)letsucc=succ_big_int(* Successor (add 1). *)letadd_int=add_int_big_int(* Addition of a small integer to a big integer. *)letsub=sub_big_int(* Subtraction. *)letpred=pred_big_int(* Predecessor (subtract 1). *)letmult=mult_big_int(* Multiplication of two big integers. *)letmult_int=mult_int_big_int(* Multiplication of a big integer by a small integer *)let_square=square_big_int(* Return the square of the given big integer *)letsqrt=sqrt_big_int(* [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=quomod_big_int(* 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=div_big_int(* Euclidean quotient of two big integers.
This is the first result [q] of [quomod_big_int] (see above). *)letmodulo=mod_big_int(* Euclidean modulus of two big integers.
This is the second result [r] of [quomod_big_int] (see above). *)letgcd=gcd_big_int(* Greatest common divisor of two big integers. *)letpower=power_big_int_positive_big_int(* 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=sign_big_int(* Return [0] if the given big integer is zero,
[1] if it is positive, and [-1] if it is negative. *)letcompare=compare_big_int(* [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=eq_big_intletle=le_big_intletge=ge_big_intletlt=lt_big_intletgt=gt_big_int(* Usual boolean comparisons between two big integers. *)letmax=max_big_int(* Return the greater of its two arguments. *)letmin=min_big_int(* Return the smaller of its two arguments. *)(* {6 Conversions to and from strings} *)letto_string=string_of_big_int(* Return the string representation of the given big integer,
in decimal (base 10). *)letof_string=big_int_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=big_int_of_int(* Convert a small integer to a big integer. *)letis_int=is_int_big_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=int_of_big_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 *)letdoublen=Z.shift_leftn1letsucc_doublen=Z.succ(Z.shift_leftn1)letpred_doublen=Z.pred(Z.shift_leftn1)letnat_casefOfSn=ifsignn<=0thenfO()elsefS(predn)letpositive_casef2p1f2pf1p=ifleponethenf1()elseletq,r=quomodptwoinifeqrzerothenf2pqelsef2p1qletn_casefOfpn=ifsignn<=0thenfO()elsefpnletz_casefOfpfnz=lets=signzinifs=0thenfO()elseifs>0thenfpzelsefn(oppz)letsgnz=Z.of_int(Z.signz)letcompare_caseelgxy=lets=comparexyinifs=0theneelseifs<0thenlelsegletnat_recfOfS=letrecloopaccn=ifsignn<=0thenaccelseloop(fSacc)(predn)inloopfOletpositive_recf2p1f2pf1=letrecloopn=iflenonethenf1elseletq,r=quomodntwoinifeqrzerothenf2p(loopq)elsef2p1(loopq)inloopletz_recfOfpfn=z_case(fun_->fO)fpfnletrecnat_rectaccfn=ifsignn<=0thenaccelsenat_rect(f()acc)f(predn)letreciter_natfnacc=ifsignn<=0thenaccelseiter_natf(predn)(facc)externalidentity:'a->'a="%identity"letshiftl_posap=Z.shift_lefta(Z.to_intp)letmodulo_posab=assert(signa>=0);assert(signb>=0);moduloabletdiv_posab=assert(signa>=0);assert(signb>0);divabletsquarea=Z.mulaaletpow_posap=Z.powa(Z.to_intp)letdiv2_truncn=Z.shift_right_truncn1letdiv_floor=Z.fdivletdiv2_floorn=Z.shift_rightn1letmod_floorab=letr=Z.remabinifStdlib.(lxor)(Z.signa)(Z.signb)>=0||Z.equalrZ.zerothenrelseZ.addbrletdiv_mod_floorab=let((p,r)aspr)=Z.div_remabinifStdlib.(lxor)(Z.signa)(Z.signb)>=0||Z.equalrZ.zerothenprelse(Z.predp,Z.addbr)letpos_div_euclab=assert(signa>=0);assert(signb>0);Z.div_remabletshiftlan=letn=Z.to_intninifn<0thenZ.shift_righta(-n)elseZ.shift_leftanletshiftran=letn=Z.to_intninifn<0thenZ.shift_lefta(-n)elseZ.shift_rightanletldiffab=Z.loganda(Z.lognotb)moduleZ=Z(* zarith *)(* Q *)(* must be already normalize *)letq_mk(num,den)={Q.den;Q.num}letq_casefq=fq.Q.denq.Q.numletq_denq=q.Q.denletq_numq=q.Q.numtypemode=NE|ZR|DN|UP|NAtypeclassify=|Zeroofbool|Infinityofbool|NaN|Finiteofbool*Z.t*Z.tletcombine_hashaccn=(n*65599)+acc