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

Implementation required for using return value in pre/post conditions #446

Open
wmanshu opened this issue Aug 1, 2019 · 3 comments
Open

Comments

@wmanshu
Copy link

wmanshu commented Aug 1, 2019

 func factorial(n: Int) -> Int
  post (n < 2 ==> (returns (1)))
  post (n >= 2 ==> (returning (r, r == n * factorial(n - 1))))
  {
    if (n < 2) { return 1 }
    return n * factorial(n: n - 1)
  }

In file factorial.flint, it is difficult to write a post condition in the case n>= 2. returning (r, r == n * factorial(n - 1)), this throws an error

Also similar examples in Voting.flint

public func getWinningProposalName() -> String
  {
    return proposals[getWinningProposalID()].name
  }
@wmanshu wmanshu changed the title Implementation required for returns which include function calls Implementation required for using return value in pre/post conditions Aug 5, 2019
@wmanshu
Copy link
Author

wmanshu commented Aug 5, 2019

one more example from flintDAO.flint

  public func executeProposal()
{
  ...
    if proposals[proposal].yea > proposals[proposal].nay {
       ...
       let totalstake: Int = getTotalStake()
       for let value: Wei in balances {
         let rawvalue: Int = (proposals[proposal].payout * value.rawValue) / totalstake
          ...
       }  
       ...
    }
}

To avoid potential divide-by-zero error, we want a condition to say that getTotalStack() > 0. But when putting it as a precondtion, it gives Flint to Boogie translation error

@mrRachar
Copy link
Collaborator

mrRachar commented Aug 5, 2019

After some discussion, we've come to the conclusion that this should not be implemented in the verification subset of the language, as it would rely on the other functions working, and so could easily produce false positives/negatives. Also, it seems against the idea of verification, surely you're not wanting to check that factorial is n * factorial(n - 1) because that is literally just the same as the code, instead you'd want to check that factorial is the product of all the numbers up to and including n.

Therefore, to allow more mathematical verification there are plans to implement product(...) and sum(...) functions, or a single reduce(op, ...) function, which can take in literal ranges or identifiers of arrays and allow you to verify these functions properly.

As for the other example, you might have to store that as a field for now to be able to verify it. Obviously, if @SusanEisenbach thinks it would be better done by creating a system for Flint function calls in Boogie, then this will be implemented.

@SusanEisenbach
Copy link
Member

SusanEisenbach commented Aug 5, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants