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

Need second argument can rely on first being false #63

Open
seebees opened this issue Dec 2, 2022 · 1 comment
Open

Need second argument can rely on first being false #63

seebees opened this issue Dec 2, 2022 · 1 comment

Comments

@seebees
Copy link
Contributor

seebees commented Dec 2, 2022

Consider the following

function Work() : Result<int, string> {
  var something := Magic();
  :- Need(|something| == 0, "Woops => " + Join(something, ","));
}

function Join(ss: seq<string>) : string
  requires 0 < |ss|

This will fail to verify.
Because Join requires 0 < |something|.
But we know (because this is how Need works,
that this condition is only important if |something| != 0.

It would be great to both rely on this.
But also to automatically thunk the second argument.
This way Dafny users can write nice clear code,
and get all the efficiency benefits in the compiler :)

@robin-aws
Copy link
Member

You can do this reasonably well in Dafny as is:

include "Wrappers.dfy"

module MoreWrappers {

  import opened Wrappers

  function method LazyNeed<E>(condition: bool, errorThunk: () --> E): (result: Outcome<E>)
    requires !condition ==> errorThunk.requires()
  {
    if condition then Pass else Fail(errorThunk())
  }

  function Work() : Result<int, string> {
    var something := Magic();
    :- LazyNeed(|something| == 0, () requires |something| > 0 => "Woops => " + Join(something, ","));
    Success(42)
  }

  function Magic(): seq<string>

  function Join(ss: seq<string>, sep: string) : string
    requires 0 < |ss|
}

The explicit lambda expression, especially with the explicit requires, is certainly a little unfortunate.

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

No branches or pull requests

2 participants