dafny-lang / ide-vscode

VSCode IDE Integration for Dafny
https://marketplace.visualstudio.com/items?itemName=dafny-lang.ide-vscode
MIT License
23 stars 18 forks source link

newtypes not recognized as subtypes for < #469

Closed erniecohen closed 6 months ago

erniecohen commented 6 months ago

Failing code

newtype P = nat
newtype Q = P
predicate test(p:P,q:Q) { p < q } // error: arguments to < must have a common supertype

Steps to reproduce the issue

Expected behavior

Actual behavior

MikaelMayer commented 6 months ago

Newtypes really define a new opaque type that is not compatible with other types except through explicit conversions. So you have to write this:

newtype P = nat
newtype Q = P
predicate test(p:P,q:Q) { p < q as P }
erniecohen commented 6 months ago

I don't think that is correct. For example, you can compare a nat (which is a newtype) to an int without complaint. As the error message says, they just have to have a common supertype, which I assume is where the relational operator is evaluated.

keyboardDrummer commented 6 months ago

For example, you can compare a nat (which is a newtype)

According to the documentation, nat is a subset type of int, which is different from a newtype.

erniecohen commented 6 months ago

Okay, I am officially senile.

MikaelMayer commented 6 months ago

Related fact: the current Dafny to Rust compiler I'm revisiting was emitting the "nat" subset type as a new type declaration and wrapper, which was wrong (it caused many issues with casting). I'm fixing it today. So you're not the only one confused with the difference between newtype and subset type.