在使用leon和理性时,我遇到了以下问题:inverse2
函数的验证给出了一个反例,但它没有多大意义,而import leon.lang._
case class Rational (n: BigInt, d: BigInt) {
def +(that: Rational): Rational = {
require(isRational && that.isRational)
Rational(n * that.d + that.n * d, d * that.d)
} ensuring { _.isRational }
def *(that: Rational): Rational = {
require(isRational && that.isRational)
Rational(n * that.n, d * that.d)
} ensuring { _.isRational }
def <=(that: Rational): Boolean = {
require(isRational && that.isRational)
if (that.d * d > 0)
n * that.d <= d * that.n
else
n * that.d >= d * that.n
}
def ==(that: Rational): Boolean = {
require(isRational && that.isRational)
//that <= this && this <= that
true // for testing purpose of course!
}
// fails to verify
def inverse1: Rational = {
require(isRational && nonZero)
Rational(d, n)
}.ensuring { res => res.isRational && res.nonZero && res * this == Rational(1, 1) }
// verifies
def inverse2: Rational = {
require(isRational && nonZero)
Rational(d, n)
}.ensuring { res => res.isRational && res.nonZero /*&& res * this == Rational(1, 1)*/ }
def isRational = !(d == 0)
def nonZero = n != 0
}
验证。
[ Info ] - Now considering 'postcondition' VC for Rational$inverse1 @33:16...
[ Error ] => INVALID
[ Error ] Found counter-example:
[ Error ] $this -> Rational(-2, -2)
leon的反例如下:
==
但从数学角度讲它是没有意义的。
我希望这段代码能够调用我已定义的true
运算符,但由于此代码总是返回{{1}}并且函数无法验证,我倾向于认为不是......
有人可以说明这个程序或我对Scala / leon的理解有什么问题吗?感谢。