Limitations of Verification¶
A goal of Stainless is to ensure that proven properties hold in all program executions so that, for example, verified programs do not crash and all of the preconditions and postconditions are true in all executions. For this to be the case, there needs to be a precise correspondence between runtime execution semantics and the semantics used in verification, including the SMT solvers invoked.
Below we document several cases where we are aware that the discrepancy exists and provide suggested workarounds.
Out of Memory Errors¶
By default, Stainless assumes that unbounded data types can be arbitrarily large and that all well-founded recursive functions have enough stack space to finish their computation. Thus a verified program may crash at run-time due to:
stack overflow
heap overflow
The set of values for recursive algebraic data types is assumed to be infinite. In any given execution, there will be actual bounds on the total available memory. The program could thus crash with an out-of-memory error when trying to allocate another value of algebraic data type.
For a safety critical application you may wish to resort to tail-recursive programs only, and also write preconditions and postconditions that enforce a bound on the maximum size of the data structures that your application manipulates. For this purpose, you can define size functions that return BigInt data types.
Equality on Any¶
On the JVM, == between two values of static type Any compares
boxed numbers by value across numeric types, and compares boxed
floating-point numbers with IEEE semantics:
val a: Any = (5: Byte)
val b: Any = 5
a == b // true on the JVM
Stainless encodes values of type Any with a structural equality in
which values of different types are never equal, so it would prove
a != b and could therefore prove properties that do not hold at
runtime.
Stainless rejects programs in which an equality may compare values of
type Any, directly or through the fields of a class, the components
of a tuple or the elements of a collection. Type parameters and abstract
type members are equality types by default: their values can be
compared with ==, and in exchange they can only be instantiated with
types that do not contain Any. The @noeq annotation lifts this
restriction for a type parameter or type member whose values are never
compared, in the same way as @mutable allows mutable type arguments:
==, literal patterns, the elements ofSetandBagand the keys ofMapcannot involve a type that containsAnyor a@noeqtype parameter, type member or class (see below);a type parameter that is not
@noeq(asTindef same[T](x: T, y: T) = x == yor inList[T]) cannot be instantiated with such a type, and an abstract type member that is not@noeqcannot be defined as such a type;a method overriding a method with a
@noeqtype parameter must annotate the parameter as well.
As Any is also a mutable type for Stainless, a type parameter must be
both @noeq and @mutable to be instantiated with Any:
def first[@noeq @mutable T](x: T, y: T): T = x
def test = first[Any](1, 2L) // ok: the values are not compared
Values of type Any remain supported as long as they are not
compared, for instance with isInstanceOf and pattern matching on
types. To compare such values, narrow them to a concrete type first.
A class can be annotated @noeq as well, when its values are never
compared: it is then treated like Any in the rules above. The
annotation spreads to the classes that would compare it: a class with a
field of a @noeq type (Any, a float, an array, a @noeq type
parameter or a @noeq class) or with a @noeq descendant, whose
values may be instances of that descendant, is @noeq too. So
Cell[@mutable @noeq T](var v: T) is @noeq: comparing two cells
would compare their contents. The fields of a @noeq class can still
be compared, and a descendant that is not annotated is an equality type:
@noeq sealed abstract class Resource
case class File(fd: BigInt) extends Resource
def test(r: Resource, f: File) =
r == r // rejected: Resource is @noeq
f == f // ok: File is not @noeq
Set(r) // rejected: the elements of a Set are compared
List(f) // ok
For the same reason, a disagreement with the JVM, == is rejected on
the values that the JVM compares by reference while Stainless compares
their contents: arrays, and the instances of a class that is not a case
class. Arrays and such classes are treated like Any in the rules
above, also when they are fields of a compared case class, and a class
cannot override equals. The elements of an array can still be
compared, and a class with member classes is exempt: Stainless gives its
instances an identity, which == compares too.
case class Buffer(data: Array[Int], size: Int)
def test(a: Array[Int], b: Buffer) =
a(0) == a(0) // ok: compares the elements
a == a.clone() // rejected: false on the JVM
b == Buffer(b.data.clone(), b.size) // rejected: compares the field data
Cell(a) // ok: the type parameter of Cell is @noeq
Float and Double are treated like Any in the rules above as
well. A == between two floats is the IEEE comparison, on the JVM and
for Stainless. But a float that is passed for a type parameter, stored
in a collection or held by a compared class or tuple is boxed on the
JVM, and boxed floats compare differently: 0.0 equals -0.0, and
a boxed NaN equals itself but no other boxed NaN, so the result does not
only depend on the values. Stainless does not model this:
def same[T](x: T, y: T): Boolean = x == y
case class P(d: Double)
def test(d: Double) =
d == d // ok: false if d is NaN
same(d, d) // rejected: T is an equality type
P(d) == P(d) // rejected: compares the field d
List(d).contains(d) // rejected: List[Double]
d match { case 0.0 => true; case _ => false } // rejected: use `d == 0.0`
A float can still be passed for a @noeq type parameter, and the
equiv method compares two floats structurally (NaN is equivalent to
itself, and 0.0 is not equivalent to -0.0).
Array, Float and Double thus behave as if they were
annotated @noeq, except that two floats can be compared directly.