@@ -15,7 +15,6 @@ import io.ksmt.sort.KRealSort
1515import io.ksmt.utils.ArithUtils.RealValue
1616import io.ksmt.utils.ArithUtils.bigIntegerValue
1717import io.ksmt.utils.ArithUtils.compareTo
18- import io.ksmt.utils.ArithUtils.modWithNegativeNumbers
1918import io.ksmt.utils.ArithUtils.numericValue
2019import io.ksmt.utils.ArithUtils.toRealValue
2120import io.ksmt.utils.uncheckedCast
@@ -231,12 +230,21 @@ fun KContext.simplifyIntMod(lhs: KExpr<KIntSort>, rhs: KExpr<KIntSort>): KExpr<K
231230 }
232231
233232 if (rValue != BigInteger .ZERO && lhs is KIntNumExpr ) {
234- return mkIntNum(modWithNegativeNumbers (lhs.bigIntegerValue, rValue))
233+ return mkIntNum(evalIntMod (lhs.bigIntegerValue, rValue))
235234 }
236235 }
237236 return mkIntModNoSimplify(lhs, rhs)
238237}
239238
239+ /* *
240+ * Eval integer mod wrt Int theory rules.
241+ * */
242+ private fun evalIntMod (a : BigInteger , b : BigInteger ): BigInteger {
243+ val remainder = a.rem(b)
244+ if (remainder >= BigInteger .ZERO ) return remainder
245+ return if (b >= BigInteger .ZERO ) remainder + b else remainder - b
246+ }
247+
240248fun KContext.simplifyIntRem (lhs : KExpr <KIntSort >, rhs : KExpr <KIntSort >): KExpr <KIntSort > {
241249 if (rhs is KIntNumExpr ) {
242250 val rValue = rhs.bigIntegerValue
@@ -246,12 +254,20 @@ fun KContext.simplifyIntRem(lhs: KExpr<KIntSort>, rhs: KExpr<KIntSort>): KExpr<K
246254 }
247255
248256 if (rValue != BigInteger .ZERO && lhs is KIntNumExpr ) {
249- return mkIntNum(lhs.bigIntegerValue.rem( rValue))
257+ return mkIntNum(evalIntRem( lhs.bigIntegerValue, rValue))
250258 }
251259 }
252260 return mkIntRemNoSimplify(lhs, rhs)
253261}
254262
263+ /* *
264+ * Eval integer rem wrt Int theory rules.
265+ * */
266+ private fun evalIntRem (a : BigInteger , b : BigInteger ): BigInteger {
267+ val mod = evalIntMod(a, b)
268+ return if (b >= BigInteger .ZERO ) mod else - mod
269+ }
270+
255271fun KContext.simplifyIntToReal (arg : KExpr <KIntSort >): KExpr <KRealSort > {
256272 if (arg is KIntNumExpr ) {
257273 return mkRealNum(arg)
0 commit comments