1515import static scala .collection .JavaConverters .asJava ;
1616import static scala .collection .JavaConverters .asJavaCollection ;
1717
18- import ap .basetypes .IdealInt ;
1918import ap .parser .IAtom ;
2019import ap .parser .IBinFormula ;
2120import ap .parser .IBinJunctor ;
4241import ap .theories .arrays .ExtArray ;
4342import ap .theories .bitvectors .ModuloArithmetic ;
4443import ap .theories .nia .GroebnerMultiplication$ ;
45- import ap .theories .rationals .Fractions ;
46- import ap .theories .rationals .Rationals$ ;
44+ import ap .theories .rationals .Rationals ;
4745import ap .types .Sort ;
4846import ap .types .Sort$ ;
4947import com .google .common .base .Preconditions ;
5048import com .google .common .collect .HashBasedTable ;
5149import com .google .common .collect .ImmutableList ;
5250import com .google .common .collect .ImmutableSet ;
5351import com .google .common .collect .Table ;
54- import java .lang .reflect .InvocationTargetException ;
5552import java .util .HashMap ;
5653import java .util .LinkedHashMap ;
5754import java .util .List ;
@@ -108,6 +105,13 @@ class PrincessFormulaCreator
108105 // modmod.bv_xnor()?
109106 // modmod.bv_comp()?
110107
108+ theoryFunctionKind .put (Rationals .addition (), FunctionDeclarationKind .ADD );
109+ theoryFunctionKind .put (Rationals .multiplication (), FunctionDeclarationKind .MUL );
110+ theoryFunctionKind .put (Rationals .multWithFraction (), FunctionDeclarationKind .MUL );
111+ theoryFunctionKind .put (Rationals .multWithRing (), FunctionDeclarationKind .MUL );
112+ theoryFunctionKind .put (Rationals .division (), FunctionDeclarationKind .DIV );
113+ theoryFunctionKind .put (Rationals .RatDivZero (), FunctionDeclarationKind .OTHER );
114+
111115 // casts to integer, sign/zero-extension?
112116
113117 theoryPredKind .put (ModuloArithmetic .bv_ult (), FunctionDeclarationKind .BV_ULT );
@@ -207,30 +211,28 @@ public Object convertValue(IExpression value) {
207211 return ((IIntLit ) value ).value ().bigIntValue ();
208212 }
209213 if (value instanceof IFunApp ) {
210- IFunApp fun = (IFunApp ) value ;
211- switch (fun .fun ().name ()) {
214+ IFunApp app = (IFunApp ) value ;
215+ switch (app .fun ().name ()) {
212216 case "true" :
213- Preconditions .checkArgument (fun .fun ().arity () == 0 );
217+ Preconditions .checkArgument (app .fun ().arity () == 0 );
214218 return true ;
215219 case "false" :
216- Preconditions .checkArgument (fun .fun ().arity () == 0 );
220+ Preconditions .checkArgument (app .fun ().arity () == 0 );
217221 return false ;
218222 case "mod_cast" :
219223 // 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 );
224+ return ((IIntLit ) app .apply (2 )).value ().bigIntValue ();
225+ case "Rat_fromRing" :
226+ Preconditions .checkArgument (app .fun ().arity () == 1 );
227+ ITerm term = app .apply (0 );
225228 if (term instanceof IIntLit ) {
226229 return ((IIntLit ) term ).value ().bigIntValue ();
227230 }
228231 break ;
229- case "_frac" :
230232 case "Rat_frac" :
231- Preconditions .checkArgument (fun .fun ().arity () == 2 );
232- ITerm term1 = fun .apply (0 );
233- ITerm term2 = fun .apply (1 );
233+ Preconditions .checkArgument (app .fun ().arity () == 2 );
234+ ITerm term1 = app .apply (0 );
235+ ITerm term2 = app .apply (1 );
234236 if (term1 instanceof IIntLit && term2 instanceof IIntLit ) {
235237 Rational ratValue =
236238 Rational .of (
@@ -241,7 +243,7 @@ public Object convertValue(IExpression value) {
241243 break ;
242244 case "str_empty" :
243245 case "str_cons" :
244- return strToString (fun );
246+ return strToString (app );
245247 default :
246248 }
247249 }
@@ -360,46 +362,39 @@ private String getName(IExpression input) {
360362 }
361363 }
362364
363- /** Returns true if the expression is a constant number. */
364- private static boolean isConstant (IFunApp pExpr ) {
365- for (IExpression sub : asJava (pExpr .args ())) {
366- if (!(sub instanceof IIntLit )) {
367- return false ;
365+ /** Returns true if the expression is a constant value. */
366+ private static boolean isValue (IExpression input ) {
367+ if (input instanceof IBoolLit || input instanceof IIntLit ) {
368+ // Boolean or integer literal
369+ return true ;
370+ } else if (input instanceof IFunApp ) {
371+ IFunApp app = (IFunApp ) input ;
372+ IFunction fun = app .fun ();
373+ if (fun .equals (Rationals .fromRing ()) || fun .equals (Rationals .frac ())) {
374+ // Rational number literal
375+ for (IExpression sub : asJava (app .args ())) {
376+ if (!(sub instanceof IIntLit )) {
377+ return false ;
378+ }
379+ }
380+ return true ;
381+ }
382+ if (fun .equals (ModuloArithmetic .mod_cast ()) && input .apply (2 ) instanceof IIntLit ) {
383+ // Bitvector literal
384+ return true ;
385+ }
386+ if (CONSTANT_UFS .contains (fun .name ())) {
387+ // Nil, Cons from String theory
388+ return true ;
368389 }
369390 }
370- return true ;
371- }
372-
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 ());
391+ return false ;
388392 }
389393
390394 @ SuppressWarnings ("deprecation" )
391395 @ Override
392396 public <R > R visit (FormulaVisitor <R > visitor , final Formula f , final IExpression input ) {
393- if (input instanceof IIntLit ) {
394- IdealInt value = ((IIntLit ) input ).value ();
395- return visitor .visitConstant (f , value .bigIntValue ());
396-
397- } else if (input instanceof IBoolLit ) {
398- IBoolLit literal = (IBoolLit ) input ;
399- return visitor .visitConstant (f , literal .value ());
400-
401- } else if (input instanceof IFunApp
402- && (isRatInt ((IFunApp ) input ) || isRatFrac ((IFunApp ) input ))) {
397+ if (isValue (input )) {
403398 return visitor .visitConstant (f , convertValue (input ));
404399
405400 } else if (input instanceof IQuantified ) {
@@ -474,20 +469,6 @@ public <R> R visit(FormulaVisitor<R> visitor, final Formula f, final IExpression
474469 return visit (visitor , f , ((IIntFormula ) input ).t ());
475470 }
476471
477- if (kind == FunctionDeclarationKind .OTHER && input instanceof IFunApp ) {
478- if (ModuloArithmetic .mod_cast ().equals (((IFunApp ) input ).fun ())
479- && ((IFunApp ) input ).apply (2 ) instanceof IIntLit ) {
480- // mod_cast(0, 256, 7) -> BV=7 with bitsize=8
481- return visitor .visitConstant (f , convertValue (input ));
482- }
483- }
484-
485- if (kind == FunctionDeclarationKind .UF && input instanceof IFunApp ) {
486- if (CONSTANT_UFS .contains (((IFunApp ) input ).fun ().name ())) {
487- return visitor .visitConstant (f , convertValue (input ));
488- }
489- }
490-
491472 ImmutableList .Builder <Formula > args = ImmutableList .builder ();
492473 ImmutableList .Builder <FormulaType <?>> argTypes = ImmutableList .builder ();
493474 int arity = input .length ();
0 commit comments