You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Description
This PR adds bounded polymorphism to the Dafny language. This feature
allows any formal type parameter to be declared with a suffix of any
number of
```
extends Type
```
clauses, where `Type` is any trait or subset type thereof. At call
sites, the actual type arguments are checked to be subtypes of the
listed type bounds. Where the formal type parameters are in scope, an
expression whose type is the type parameter can be converted to a
supertype of any of the type bounds by an explicit type conversion
(`as`). In some cases, the type conversion is inferred automatically and
can then be omitted from the program text.
The feature requires `--type-system-refresh` for full functionality.
Closes#5211
# Example
For `test.dfy` being the program
``` dafny
trait Printable {
method Print()
}
method SelectPrintReturn<T extends Printable>(operateOnLeft: bool, left: T, right: T) returns (r: T)
{
r := if operateOnLeft then left else right;
(r as Printable).Print();
}
datatype Dt extends Printable = Dt(u: int) {
method Print() {
print u, "\n";
}
}
method Main() {
var d0 := Dt(500);
var d1 := Dt(702);
var e0 := SelectPrintReturn(true, d0, d1); // 500
var e1 := SelectPrintReturn(false, d0, d1); // 702
print (d0, d1) == (d0, d1), "\n"; // true
}
```
we get:
```
> dafny run test.dfy --type-system-refresh --general-traits=datatype
Dafny program verifier finished with 3 verified, 0 errors
500
702
true
```
# Bonus
The PR also
* suppresses near-identical error messages (in particular, it reports
only one error for conditions that split into several checks, like the
type equality `A==B` which turns into both `A :> B` and `B >: A`)
<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
0 commit comments