4242import ap .theories .arrays .ExtArray ;
4343import ap .theories .bitvectors .ModuloArithmetic ;
4444import ap .theories .nia .GroebnerMultiplication$ ;
45- import ap .theories .rationals .Fractions ;
46- import ap .theories .rationals .Rationals$ ;
45+ import ap .theories .rationals .Rationals ;
4746import ap .types .Sort ;
4847import ap .types .Sort$ ;
4948import com .google .common .base .Preconditions ;
5049import com .google .common .collect .HashBasedTable ;
5150import com .google .common .collect .ImmutableList ;
5251import com .google .common .collect .ImmutableSet ;
5352import com .google .common .collect .Table ;
54- import java .lang .reflect .InvocationTargetException ;
5553import java .util .HashMap ;
5654import java .util .LinkedHashMap ;
5755import java .util .List ;
@@ -108,6 +106,13 @@ class PrincessFormulaCreator
108106 // modmod.bv_xnor()?
109107 // modmod.bv_comp()?
110108
109+ theoryFunctionKind .put (Rationals .addition (), FunctionDeclarationKind .ADD );
110+ theoryFunctionKind .put (Rationals .multiplication (), FunctionDeclarationKind .MUL );
111+ theoryFunctionKind .put (Rationals .multWithFraction (), FunctionDeclarationKind .MUL );
112+ theoryFunctionKind .put (Rationals .multWithRing (), FunctionDeclarationKind .MUL );
113+ theoryFunctionKind .put (Rationals .division (), FunctionDeclarationKind .DIV );
114+ theoryFunctionKind .put (Rationals .RatDivZero (), FunctionDeclarationKind .OTHER );
115+
111116 // casts to integer, sign/zero-extension?
112117
113118 theoryPredKind .put (ModuloArithmetic .bv_ult (), FunctionDeclarationKind .BV_ULT );
@@ -207,30 +212,28 @@ public Object convertValue(IExpression value) {
207212 return ((IIntLit ) value ).value ().bigIntValue ();
208213 }
209214 if (value instanceof IFunApp ) {
210- IFunApp fun = (IFunApp ) value ;
211- switch (fun .fun ().name ()) {
215+ IFunApp app = (IFunApp ) value ;
216+ switch (app .fun ().name ()) {
212217 case "true" :
213- Preconditions .checkArgument (fun .fun ().arity () == 0 );
218+ Preconditions .checkArgument (app .fun ().arity () == 0 );
214219 return true ;
215220 case "false" :
216- Preconditions .checkArgument (fun .fun ().arity () == 0 );
221+ Preconditions .checkArgument (app .fun ().arity () == 0 );
217222 return false ;
218223 case "mod_cast" :
219224 // we found a bitvector BV(lower, upper, ctxt), lets extract the last parameter
220- return ((IIntLit ) fun .apply (2 )).value ().bigIntValue ();
221- case "_int" :
222- case "Rat_int" :
223- Preconditions .checkArgument (fun .fun ().arity () == 1 );
224- ITerm term = fun .apply (0 );
225+ return ((IIntLit ) app .apply (2 )).value ().bigIntValue ();
226+ case "Rat_fromRing" :
227+ Preconditions .checkArgument (app .fun ().arity () == 1 );
228+ ITerm term = app .apply (0 );
225229 if (term instanceof IIntLit ) {
226230 return ((IIntLit ) term ).value ().bigIntValue ();
227231 }
228232 break ;
229- case "_frac" :
230233 case "Rat_frac" :
231- Preconditions .checkArgument (fun .fun ().arity () == 2 );
232- ITerm term1 = fun .apply (0 );
233- ITerm term2 = fun .apply (1 );
234+ Preconditions .checkArgument (app .fun ().arity () == 2 );
235+ ITerm term1 = app .apply (0 );
236+ ITerm term2 = app .apply (1 );
234237 if (term1 instanceof IIntLit && term2 instanceof IIntLit ) {
235238 Rational ratValue =
236239 Rational .of (
@@ -241,7 +244,7 @@ public Object convertValue(IExpression value) {
241244 break ;
242245 case "str_empty" :
243246 case "str_cons" :
244- return strToString (fun );
247+ return strToString (app );
245248 default :
246249 }
247250 }
@@ -360,8 +363,11 @@ private String getName(IExpression input) {
360363 }
361364 }
362365
363- /** Returns true if the expression is a constant number. */
364- private static boolean isConstant (IFunApp pExpr ) {
366+ /** Returns true if the expression is a constant value. */
367+ private static boolean isValue (IFunApp pExpr ) {
368+ if (!pExpr .fun ().equals (Rationals .frac ()) && !pExpr .fun ().equals (Rationals .fromRing ())) {
369+ return false ;
370+ }
365371 for (IExpression sub : asJava (pExpr .args ())) {
366372 if (!(sub instanceof IIntLit )) {
367373 return false ;
@@ -370,23 +376,6 @@ private static boolean isConstant(IFunApp pExpr) {
370376 return true ;
371377 }
372378
373- /** Returns true if the expression is an integer literal. */
374- private static boolean isRatInt (IFunApp pExpr ) {
375- // We need to use reflection to get Rationals.int() as `int` can't be a method name in Java
376- final IFunction ratInt ;
377- try {
378- ratInt = (IFunction ) Fractions .class .getMethod ("int" ).invoke (Rationals$ .MODULE$ );
379- } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex ) {
380- throw new RuntimeException (ex );
381- }
382- return isConstant (pExpr ) && pExpr .fun ().equals (ratInt );
383- }
384-
385- /** Returns true if the expression is a faction literal. */
386- private static boolean isRatFrac (IFunApp pExpr ) {
387- return isConstant (pExpr ) && pExpr .fun ().equals (Rationals$ .MODULE$ .frac ());
388- }
389-
390379 @ SuppressWarnings ("deprecation" )
391380 @ Override
392381 public <R > R visit (FormulaVisitor <R > visitor , final Formula f , final IExpression input ) {
@@ -398,8 +387,7 @@ public <R> R visit(FormulaVisitor<R> visitor, final Formula f, final IExpression
398387 IBoolLit literal = (IBoolLit ) input ;
399388 return visitor .visitConstant (f , literal .value ());
400389
401- } else if (input instanceof IFunApp
402- && (isRatInt ((IFunApp ) input ) || isRatFrac ((IFunApp ) input ))) {
390+ } else if (input instanceof IFunApp && (isValue ((IFunApp ) input ))) {
403391 return visitor .visitConstant (f , convertValue (input ));
404392
405393 } else if (input instanceof IQuantified ) {
0 commit comments