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

Dafny 4.0 suggestion: for i <- 0 to 10 instead of for i := 0 to 10 #2341

Open
MikaelMayer opened this issue Jun 30, 2022 · 4 comments
Open
Labels
breaking-change Any change that will cause existing Dafny codebases to break (excluding verification instability) misc: language proposals Proposals for change to the language that go beyon simple enhancement requests part: language definition Relating to the Dafny language definition itself

Comments

@MikaelMayer
Copy link
Member

Currently, to write a for loop, we use

for i := 0 to 10 {
}

Since the introduction of sequences, it would be nicer to be able to write

for i <- 0 to 10 {
}

or simply more unambiguously

for i | 0 <= i < 10 {
}
@MikaelMayer MikaelMayer added part: language definition Relating to the Dafny language definition itself breaking-change Any change that will cause existing Dafny codebases to break (excluding verification instability) labels Jun 30, 2022
@cpitclaudel cpitclaudel added the misc: language proposals Proposals for change to the language that go beyon simple enhancement requests label Jul 2, 2022
@robin-aws
Copy link
Member

for i <- 0 to 10 {
}

I definitely like this, since it would be consistent with the follow-up idea we had from the ordered quantification RFC of supporting the same thing in foreach loops and sequence comprehensions (e.g. seq i <- 0 to 10 :: i * i).

It does mean that foreach i <- 0 to 10 { } would mean exactly the same thing though, so unless having for as a separate, more limited form makes verification MUCH easier (unlikely since we could keep the current encoding as a special case), I'd be inclined to deprecate and drop for at that point.

So my preference would be to add support for lo to hi as a quantifier variable domain (possibly by supporting it as independent syntax for sequence values). I've cut that as a separate issue: #2583

for i | 0 <= i < 10 {
}

This we shouldn't do, since you only have a well-defined ordering for quantification if you declare a domain for the variable (i.e. i <- <C>). For an arbitrary type T, there isn't an obvious way to order values that match for t: T | P(t) {}.

@MikaelMayer
Copy link
Member Author

I'm in for keeping only the first one then.

@davidcok
Copy link
Collaborator

davidcok commented Sep 6, 2022

Use .. instead of 'to

@robin-aws
Copy link
Member

Will also discuss the larger possibility of replacing for with foreach (which needs significant discussion)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Any change that will cause existing Dafny codebases to break (excluding verification instability) misc: language proposals Proposals for change to the language that go beyon simple enhancement requests part: language definition Relating to the Dafny language definition itself
Projects
None yet
Development

No branches or pull requests

4 participants