Leon:如何使用自定义`==`运算符?

时间:2015-05-25 12:41:39

标签: scala leon

在使用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的理解有什么问题吗?感谢。

1 个答案:

答案 0 :(得分:3)

我担心这是不可能的。正如您在源代码中看到的herehere一样,Leon将==视为普通的方法调用,但它会变成一个名为{{的特殊AST节点1}}。

但是有一个简单的解决方法:只需调用您的平等Equals而不是===