Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DMS-34] motoko-san: add basic support for Nat #7

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

int-index
Copy link
Member

New features:

  • translate Nat to Int

New test cases:

  • test/viper/nats.mo

Future work: encode in Viper that Nats are non-negative.

New features:
* translate Nat to Int

New test cases:
* test/viper/nats.mo

Future work: encode in Viper that Nats are non-negative.
@GoPavel
Copy link

GoPavel commented Apr 25, 2024

I think this is sound translation because the translation is performing after typechecking.
Probably a good next step is to add auto deducing precondition (easy) and loop invariant (a bit harder).
Example:

private func foo(x: Nat)

translated into

method foo(x: Int)
  requries x >= 0

and

var i : Nat = 0
while (i < arr.size()) {
     // assert:invariant i >= 0  // auto inferred 
    arr[i] := i
    i := i + 1
}

I think this is useful general scheme (kinda refinement types). It will help support in the future int8 int16 and so on.

WDYT?
Probably we can postpone it for a next milestone

@GoPavel
Copy link

GoPavel commented Apr 25, 2024

Except comment above LGTM

@int-index
Copy link
Member Author

method foo(x: Int)
  requries x >= 0

kinda refinement types

Yeah, I've been thinking along these lines too. n : Nat is basically { n : Int | n >= 0 }, so we could encode this refinement type with predicates in Viper.

For Nat8, Nat16, etc, it's more tricky because arithmetic operations can overflow, so we'd have to translate a + b to (a + b) % 256 and (a + b) % 65536 respectively. Signed overflow (Int8, Int16, etc) is even trickier.

@int-index
Copy link
Member Author

Probably we can postpone it for a next milestone

Yes, let's postpone because it would take some effort to get it right. For example:

  • What happens when we have an array of Nat? That is, how should the requires clause look for ns : [Nat]?
  • What happens if we emit ensures $Res >= 0 but the solver can't prove it?
  • How do we track those predicates in the translator? How do we know that a loop invariant is needed?

We could probably solve such issues, but let's get some basic support for Nat first and enhance it later.

@int-index int-index merged commit 7804c5c into master Apr 25, 2024
3 of 5 checks passed
@delete-merged-branch delete-merged-branch bot deleted the motoko-san/prim-nat branch April 25, 2024 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants