123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242(******************************************************************************)(* *)(* The Alt-Ergo theorem prover *)(* Copyright (C) 2006-2013 *)(* *)(* Sylvain Conchon *)(* Evelyne Contejean *)(* *)(* Francois Bobot *)(* Mohamed Iguernelala *)(* Stephane Lescuyer *)(* Alain Mebsout *)(* *)(* CNRS - INRIA - Universite Paris Sud *)(* *)(* This file is distributed under the terms of the Apache Software *)(* License version 2.0 *)(* *)(* ------------------------------------------------------------------------ *)(* *)(* Alt-Ergo: The SMT Solver For Software Verification *)(* Copyright (C) 2013-2018 --- OCamlPro SAS *)(* *)(* This file is distributed under the terms of the Apache Software *)(* License version 2.0 *)(* *)(******************************************************************************)(** Integers implementation. Based on Zarith's integers **)moduleZ:NumbersInterface.ZSigwithtypet=Big_int.big_int=structopenBig_inttypet=big_intletzero=zero_big_intletone=unit_big_intletm_one=minus_big_intoneletcompareab=compare_big_intabletcompare_to_0a=sign_big_intaletequalab=eq_big_intabletis_zerot=compare_to_0t=0letis_onet=equaltoneletis_m_onet=equaltm_oneletsignt=sign_big_inttlethasht=Hashtbl.hashtletaddab=add_big_intabletmultab=mult_big_intabletabst=abs_big_inttletsubab=sub_big_intabletminust=minus_big_inttletdivab=assert(not(is_zerob));div_big_intabletmaxab=max_big_intabletto_stringt=string_of_big_inttletfrom_strings=big_int_of_stringsletfrom_intn=big_int_of_intnletremab=assert(not(is_zerob));mod_big_intabletdiv_remab=assert(not(is_zerob));quomod_big_intabletprintfmtt=Format.fprintffmt"%s"(to_stringt)letmy_gcdab=ifis_zeroathenbelseifis_zerobthenaelsegcd_big_intabletmy_lcmab=trydiv(multab)(my_gcdab)withe->Format.printf"my_lcm %a %a failed with:@.%s@."printaprintb(Printexc.to_stringe);assertfalseletto_floatt=float_of_big_inttletto_machine_intt=trySome(Big_int.int_of_big_intt)with_->Noneletfdivab=assert(not(is_zerob));letopenNumintryletn1=num_of_big_intainletn2=num_of_big_intbinletnm=div_numn1n2inbig_int_of_num(floor_numnm)withe->Format.printf"fdiv %a %a failed with:@.%s@."printaprintb(Printexc.to_stringe);assertfalseletcdivab=assert(not(is_zerob));letopenNumintryletn1=num_of_big_intainletn2=num_of_big_intbinletnm=div_numn1n2inbig_int_of_num(ceiling_numnm)withe->Format.printf"cdiv %a %a failed with:@.%s@."printaprintb(Printexc.to_stringe);assertfalseletpoweran=assert(n>=0);power_big_int_positive_intan(* Shifts left by (n:int >= 0) bits. This is the same as t * pow(2,n) *)letshift_left=shift_left_big_int(* returns sqrt truncated with the remainder. It assumes that the argument
is positive, otherwise, [Invalid_argument] is raised. *)letsqrt_remt=letsq=sqrt_big_inttinsq,subt(multsqsq)lettestbitzn=assert(n>=0);is_one(extract_big_intzn1)letnumbits=Big_int.num_bits_big_intend(** Rationals implementation. Based on Zarith's rationals **)moduleQ:NumbersInterface.QSigwithmoduleZ=Z=structmoduleZ=ZexceptionNot_a_floatopenNumtypet=numletzero=Int0letone=Int1letm_one=Int(-1)letof_intn=Intnletcompare_to_0n=sign_numnletis_zeron=compare_to_0n=0letequalab=a=/bletis_onen=equalonenletis_m_onen=equalm_onenletceiling=ceiling_numletfloor=floor_numletis_int=is_integer_numletabs=abs_numletpoweran=ifn=0thenone(* 0 ^ 0 = 1, undefined in mathematics*)elsematchawith|Int1->one|Int0->zero|Int(-1)->ifnmod2=0thenoneelsem_one|_->power_numa(Intn)letmodulo=mod_numletdivab=assert(not(is_zerob));div_numabletmult=mult_numletsub=sub_numletadd=add_numletminus=minus_numletsign=sign_numletcompare=compare_numletequalab=a=/bletto_string=string_of_numletfrom_string=num_of_stringletto_float=float_of_numletto_z=big_int_of_numletfrom_z=num_of_big_intletfrom_inti=num_of_intiletden=function|Int_|Big_int_->Big_int.unit_big_int|Ratiorat->Ratio.denominator_ratioratletnum=function|Inti->Big_int.big_int_of_inti|Big_intb->b|Ratiorat->Ratio.numerator_ratioratletfrom_floatx=ifx=infinity||x=neg_infinitythenraiseNot_a_float;let(f,n)=frexpxinletz=Big_int.big_int_of_string(Int64.to_string(Int64.of_float(f*.2.**52.)))inletfactor=power(of_int2)(n-52)inmult(from_zz)factorlethashv=matchvwith|Inti->i|Big_intb->Hashtbl.hashb|Ratiorat->Hashtbl.hash(Ratio.normalize_ratiorat)letprintfmtq=Format.fprintffmt"%s"(to_stringq)letmint1t2=min_numt1t2letmaxt1t2=max_numt1t2letinvt=ifZ.is_zero(numt)thenraiseDivision_by_zero;one//tletfrom_zzz1z2=Big_intz1//Big_intz2(********
comparer avec l'implem de Alain de of_float
let ratio_of_float f =
Ratio.ratio_of_string (string_of_float f)
let num_of_float f = num_of_ratio (ratio_of_float f)
let of_float x =
let res = of_float x in
let z = num_of_float x in
assert (res =/ z);
res
********)lettruncatet=letres=integer_numtinassert(compare(absres)(abst)<=0);matchreswith|Inti->Big_int.big_int_of_inti|Big_intb->b|Ratio_->assertfalseletmult_2exptn=multt(power(Int2)n)letdiv_2exptn=divt(power(Int2)n)end